Bruce van der Kooij wrote: > The bad news? Well, it doesn't really return the currently selected > items but the items that were last selected What was I smoking? I've attached a new version which makes sure it definitely returns what is _currently_ last selected. If Nautilus loses the selection (e.g. by deselecting) I just send an empty array over the DBus. Best regards, Bruce
# Wrote this up in some 30 minutes, allows other programs to query # a DBus service running for which items are selected in Nautilus. # # Here's a typical session: # # Start the service: # $ python NautilusDBus.py # # Start Nautilus: # $ nautilus -q; nautilus --no-desktop . # # Communicate with the DBus service from your application: # # import dbus # import dbus.service # # INTERFACE = "org.google.code.nautilusdbus.Service" # OBJECT_PATH = "/org/google/code/nautilusdbus/Service" # SERVICE = "org.google.code.nautilussvn.NautilusDBus" # # session_bus = dbus.SessionBus() # dbus_service = session_bus.get_object(SERVICE, OBJECT_PATH) # # for path in dbus_service.GetSelectedPaths(): print path # import traceback # Normally one wouldn't do this but this is a quick hack and I want # everything in a single file (the nautilus module is only available # when running inside Nautilus). if __name__ != "__main__": import nautilus import gobject import dbus import dbus.service import dbus.mainloop.glib INTERFACE = "org.google.code.nautilusdbus.Service" OBJECT_PATH = "/org/google/code/nautilusdbus/Service" SERVICE = "org.google.code.nautilussvn.NautilusDBus" if __name__ != "__main__": class NautilusDBus(nautilus.MenuProvider): def __init__(self): # Connect to the DBus service self.session_bus = dbus.SessionBus() self.dbus_service = self.session_bus.get_object(SERVICE, OBJECT_PATH) def get_file_items(self, window, items): if len(items) == 0: # When you deselect everything (e.g. click on the background) # get_file_items is called with 0 items, we can't pass a # normal empty Python list though because DBus needs to # know the exact type. self.dbus_service.SetSelectedPaths(dbus.Array([], "as")) return [] # We could convert the uris to actual paths but hey who cares? :-) self.dbus_service.SetSelectedPaths([item.get_uri() for item in items]) return [] # Don't think we actually have to return anything per se class Service(dbus.service.Object): selected_paths = [] def __init__(self, connection): dbus.service.Object.__init__(self, connection, OBJECT_PATH) @dbus.service.method(INTERFACE, in_signature="as", out_signature="") def SetSelectedPaths(self, paths): self.selected_paths = paths @dbus.service.method(INTERFACE, in_signature="", out_signature="as") def GetSelectedPaths(self): # Returns a dbus.Array (as, array of strings) return self.selected_paths if __name__ == "__main__": dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) session_bus = dbus.SessionBus() name = dbus.service.BusName(SERVICE, session_bus) service = Service(session_bus) mainloop = gobject.MainLoop() mainloop.run()
Attachment:
signature.asc
Description: OpenPGP digital signature