Question

I've an application written using PyGTK (GTK+2). I'd like to integrate it with Nautilus via an extension (something I am trying to learn). My current desktop has GNOME3 and Nautilus 3, which is written in GTK+3 and the extensions for Nautilus uses PyGObject.

Can I integrate my application in GTK+2 with Nautilus 3? (without porting my application to GTK+3, yet). Any hint?

I'm planning to port my application to GTK+3 (PyGObject), but it'll require more time than I have now.

Was it helpful?

Solution

Yes, it is possible. For instance, you can use Nautilus to call your program with the files or directories as arguments. The program you are calling can be written with any toolkit, or even be just a shell script.

A tiny example or an extension:

from gi.repository import Nautilus, GObject
from urllib import unquote

PROGRAM_NAME = '/path/to/your/program'

class MyExtension(GObject.GObject, Nautilus.MenuProvider):
    def __init__(self):
        pass

    def call_my_program(self, menu, files):
        # Do whatever you want to do with the files selected
        if len(files) == 0:
            return

        # Strip the URI format to plain file names
        names = [ unquote(file.get_uri()[7:]) for file in files ]

        argv = [ PROGRAM_NAME ] + names

        GObject.spawn_async(argv, flags=GObject.SPAWN_SEARCH_PATH)

    def get_file_items(self, window, files):
        # Show the menu if there is at least on file selected
        if len(files) == 0:
            return

        # We care only files (local files)            
        for fd in files:
            if fd.is_directory() or fd.get_uri_scheme() != 'file':
                return

        item = Nautilus.MenuItem(name='MyExtensionID::MyMethodID',
                                 label='Do something with my program...')
        item.connect('activate', self.call_my_program, files)

        return item,

The extension is written using GObject Introspection (Nautilus 3), and it is generic: you can call any external program you want that accepts files as arguments. The key is GObject.spawn_async().

get_file_items is the method that Nautilus call when the user interacts with files. In that, you can bind a contextual menu (with Nautilus.MenuItem()). Then, you connect that menu with the method that calls your program (call_my_program()).

You can create other filters in the method get_file_items. For instance, to show the contextual menu only if there are text plain files selected (using fd.is_mime_type()). You can do whatever you have in mind. Beware of performing only non-blocking operations, otherwise you could block Nautilus.

To test the extension, you can install it in ~/.local/share/nautilus-python/extensions.

OTHER TIPS

Check Introspection Porting:

Note that you can't do a migration halfway: If you try to import both gtk and gi.repository.Gtk, you'll get nothing but program hangs and crashes, as you are trying to work with the same library in two different ways. You can mix static and GI bindings of different libraries though, such as dbus-python and gi.repository.Gtk.

So, it depends on how the Nautilus plugins are implemented.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top