7

If I have a bus name, an object path, and an interface, how do I call DBus methods from Gjs (in a gnome-shell extension)?

I'm looking for the equivalent of the following python code:

import dbus
bus = dbus.SessionBus()
obj = bus.get_object("org.gnome.Caribou.Keyboard", "/org/gnome/SessionManager/EndSessionDialog")
obj.Open(0, 0, 120, dbus.Array(signature="o"))

(Note that I didn't explicitly use the interface due to some python-dbus magic, but I could have with iface = dbus.interface(obj, "org.gnome.SessionManager.EndSessionDialog"). Since I have the interface name, I'm fine with a solution that queries it. Also note that this example would be silly in Gjs, as it calls back into gnome-shell)

jdm
  • 9,470
  • 12
  • 58
  • 110

2 Answers2

13

The import imports.dbus is deprecated since gnome-shell 3.4. The new way is to use Gio as described here:

const Gio = imports.gi.Gio;

const MyIface = '<interface name="org.example.MyInterface">\
<method name="Activate" />\
</interface>';
const MyProxy = Gio.DBusProxy.makeProxyWrapper(MyIface);

let instance = new MyProxy(Gio.DBus.session, 'org.example.BusName',
'/org/example/Path');

(Note that the original post uses makeProxyClass, correct is makeProxyWrapper.)

You can get the interface definition, for example, by using introspection. For pidgin/purple do:

$ dbus-send --print-reply --dest=im.pidgin.purple.PurpleService \
/im/pidgin/purple/PurpleObject org.freedesktop.DBus.Introspectable.Introspect

Further explanations on introspection and inspection of interfaces can be found here.

nemo
  • 55,207
  • 13
  • 135
  • 135
1

this should give you a better idea:

gjs> const DBus = imports.dbus;
gjs> for (let i in DBus) log(i);
spirit walker
  • 304
  • 1
  • 6