Created
October 14, 2017 22:42
-
-
Save iwconfig/c7505835ed94a96f9b32b2326768e46c to your computer and use it in GitHub Desktop.
Open terminal in current or selected folder in Nautilus file manager. Supports locales - please add your transation(s) if you want.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# NOTE: This is based on 'open-terminal.py' example originally written by Martin Enlund. My version is a bit more comprehensive. | |
# - Locales (please feel free to add more) | |
# - Tries to start the default terminal application in multiple different ways (all i could think of at the moment, please add more [self.executables]) | |
# - Display different label depending on what is selected. | |
# Background and file has the same label, and directory has its own. | |
# | |
# URLs: https://github.com/iwconfig | |
# https://www.apt-browse.org/browse/ubuntu/trusty/universe/i386/python-nautilus/1.1-4/file/usr/share/doc/python-nautilus/examples/open-terminal.py | |
# | |
# INSTALL: Save this file in ~/.local/share/nautilus-python/extensions | |
import os | |
import urllib | |
import locale | |
from gi.repository import Nautilus, GObject | |
class OpenTerminalExtension(Nautilus.MenuProvider, GObject.GObject): | |
def __init__(self): | |
self.locales = { 'en_*': ('english', | |
['Open in terminal','Open terminal in'], | |
['Open terminal here', 'Open terminal in current directory']), | |
'sv_SE': ('swedish', | |
['Öppna i terminalen', 'Öppna terminalen i'], | |
['Öppna terminalen här', 'Öppna terminalen i den nuvarande sökvägen']) } | |
self.locale = next(self.locales[lc] for lc in self.locales if lc.strip('*') in locale.getdefaultlocale()[0]) | |
self.executables = ['i3-sensible-terminal', 'exo-open --launch TerminalEmulator', | |
'x-terminal-emulator', 'gsettings get org.gnome.desktop.default-applications.terminal'] | |
def _open_terminal(self, file): | |
if file.is_directory() or file.get_uri_scheme() != 'file': | |
file = file.get_uri() | |
else: | |
file = file.get_parent_uri() | |
filename = urllib.unquote(file[7:]) | |
os.chdir(filename) | |
for x in self.executables: | |
if os.system('{} &'.format(x)) == 0: | |
break | |
def menu_activate_cb(self, menu, file): | |
self._open_terminal(file) | |
def menu_background_activate_cb(self, menu, file): | |
self._open_terminal(file) | |
def file_folder_or_bg_menu(self, file, bg=False): | |
if not bg and file.get_mime_type() == 'inode/directory': | |
item = Nautilus.MenuItem(name='NautilusPython::openterminal_file_item', | |
label=self.locale[1][0], | |
tip='{0} {1}'.format(self.locale[1][1], file.get_name())) | |
menu = self.menu_activate_cb | |
else: | |
item = Nautilus.MenuItem(name='NautilusPython::openterminal_item', | |
label=self.locale[2][0], | |
tip=self.locale[2][1]) | |
menu = self.menu_background_activate_cb | |
item.connect('activate', menu, file) | |
return item | |
def get_file_items(self, window, file): | |
if len(file) != 1: | |
return | |
item = self.file_folder_or_bg_menu(file[0]) | |
return item, | |
def get_background_items(self, window, file): | |
item = self.file_folder_or_bg_menu(file, bg=True) | |
return item, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment