#!/usr/bin/env python # -*- coding: utf-8 -*- """This module adds a menu item to the nautilus right-click menu which allows to Open the Terminal on the Selected Folder/Current Directory at predefined Geometry just through the right-clicking""" # open-terminal-geometry.py version 1.0.5 # # Copyright 2009-2010 Giuseppe Penone # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. import pygtk pygtk.require('2.0') import gtk import nautilus, gconf, urllib, os, sys, subprocess GEOMETRY = '160x20' NAUTILUS_MENU_LABEL = 'Open Terminal Here' NAUTILUS_MENU_HELP_ON_SELECTED = 'Open the Terminal on the Current/Selected Directory' NAUTILUS_MENU_HELP_ON_BACKGROUND = 'Open the Terminal on the Current Directory' def error_dialog(message, dialog_title = "Error..."): """The extension's error dialog""" dialog = gtk.MessageDialog(flags=gtk.DIALOG_MODAL, type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK, message_format=message) dialog.set_title(dialog_title) dialog.run() dialog.destroy() class OpenTerminalGeometry(nautilus.MenuProvider): """Implements the 'Open Terminal Geometry' extension to the nautilus right-click menu""" def __init__(self): """Creates a GConfClient (Gnome Configuration Client)""" self.client = gconf.client_get_default() def run(self, menu, selected): """Runs the Open Terminal Geometry on the given Directory""" curr_dir = urllib.unquote(selected.get_uri()[7:]) if os.path.isfile(curr_dir): curr_dir = os.path.dirname(curr_dir) try: bash_string = "gnome-terminal --geometry=" + GEOMETRY + " --working-directory='" + curr_dir + "' &" subprocess.call(bash_string, shell=True) except: error_dialog("Exception Error:\n%s" % sys.exc_info()) def get_file_items(self, window, sel_items): """Adds the 'Open Terminal Geometry' menu item to the Nautilus right-click menu, connects its 'activate' signal to the 'run' method passing the selected Directory/File""" if len(sel_items) != 1 or sel_items[0].get_uri_scheme() != 'file': return item = nautilus.MenuItem('NautilusPython::terminal', NAUTILUS_MENU_LABEL, NAUTILUS_MENU_HELP_ON_SELECTED) item.set_property('icon', 'terminal') item.connect('activate', self.run, sel_items[0]) return item, def get_background_items(self, window, current_directory): """Adds the 'Open Terminal Geometry' menu item to the Nautilus right-click menu, connects its 'activate' signal to the 'run' method passing the current Directory""" item = nautilus.MenuItem('NautilusPython::terminal', NAUTILUS_MENU_LABEL, NAUTILUS_MENU_HELP_ON_BACKGROUND) item.set_property('icon', 'terminal') item.connect('activate', self.run, current_directory) return item,