Open Terminator from nautilus context menu

Posted . Visible to the public.

On our Ubuntu machines we have nautilus file manager with nautilus-extension-gnome-terminal installed. This adds an entry to the context menu (right click) to start a gnome-terminal in the current directory. As I'm mostly using Terminator terminal, I wanted to have a similar context menu entry to launch Terminator directly. I came across this python script Show archive.org snapshot that does exactly that.

  • Install python3-nautilus: sudo apt install python3-nautilus
  • Create /usr/share/nautilus-python/extensions/nautilus-open-in-terminal.py with content
import os, locale

try:
    from urllib import unquote
except ImportError:
    from urllib.parse import unquote

from gi import require_version
require_version('Gtk', '3.0')
require_version('Nautilus', '3.0')

from gi.repository import Nautilus, GObject
from gettext import gettext, bindtextdomain, textdomain

TERMINAL_PATH="/usr/bin/terminator"

class NautilusOpenInTerminal(GObject.GObject, Nautilus.MenuProvider):
	"""Open in terminator"""
	def __init__(self):
		GObject.Object.__init__(self)

	def get_background_items(self, window, file):
		"""Returns the menu items to display when no file/folder is selected
		(i.e. when right-clicking the background)."""

		# Add the menu items
		items = []
		self._setup_gettext();
		self.window = window
		if file.is_directory() and file.get_uri_scheme() == "file":
			if os.path.exists(TERMINAL_PATH):
				items += [self._create_terminal_item(file)]

		return items


	def _setup_gettext(self):
		"""Initializes gettext to localize strings."""
		try: # prevent a possible exception
			locale.setlocale(locale.LC_ALL, "")
		except:
			pass
		bindtextdomain("nautilus-open-in-terminal", "/usr/share/locale")
		textdomain("nautilus-open-in-terminal")

	def _create_terminal_item(self, file):
		"""Creates the 'Open in Terminal' menu item."""
		item = Nautilus.MenuItem(name="NautilusOpenInTerminal::Nautilus",
		                         label=gettext("Open in T_erminator"),
		                         tip=gettext("Open this folder in the Terminator terminal"))
		item.connect("activate", self._terminal_run, file)
		return item


	def _terminal_run(self, menu, file):
		"""'Open in Terminal' menu item callback."""
		filename = unquote(file.get_uri()[7:])
		os.chdir(filename)
		os.system(TERMINAL_PATH + " &")
  • restart nautilus: nautilus -q
Profile picture of Daniel Straßner
Daniel Straßner
Last edit
Daniel Straßner
License
Source code in this card is licensed under the MIT License.
Posted by Daniel Straßner to makandra dev (2024-10-14 12:36)