The problem is, print("Testing"); works., But print("Connection"); does not print anything in the stdout. I can not print anything from within the callback function of Gio.DBus.session.call.
How can I get the return value of dbus call from gjs script.
Note that you will also need to ensure you are running a GLib.MainLoop (or iterating the main context some other way) or the callback will never be invoked. In that case, you should probably review the Asynchronous Programming tutorial as well.
I have tried (trial and error) many combinations. And also went through the tutorial (in fact the code of my question is from the tutorial page). I think the code is now ok after your suggestions. But how to connect it to the main loop.
(gjs:121339): Gjs-WARNING **: 08:25:18.802: JS ERROR: Gio.DBusError: The name org.gnome.Shell.Extensions.GnomeUtilsWorkspaces was not provided by any .service files
sourceId</<@./test:25:46
@./test:40:6
In the extension where I have defined GetWorkspaces, there is a variable like:
This is telling that the service you are trying to call a method on is not exported on D-Bus at org.gnome.Shell.Extensions.GnomeUtilsWorkspaces.
You’ll want to go through tutorial, where it explains the difference between well-known names, objects and interfaces, then move on to Owning a Name if you want your own service name. I’d recommend you read the full tutorial though, if you’re new to D-Bus.
So, how the service i am trying to call a method on is not exported on D-Bus?
The extension I am using is located in:
I have gone through the pages you have linked. Maybe i am missing something. Without working code, it is hard for me understand how the pieces ( Gio.DBus.session.call, GLib.MainLoop, …) come together.
To be clearer about what happened here, you exported your interface (org.gnome.Shell.Extensions.GnomeUtilsWorkspaces) at an object path (/org/gnome/Shell/Extensions/GnomeUtilsWorkspaces).
Because the gnome-shell process already owns the well-known name org.gnome.Shell, any calls to org.gnome.Shell will be routed to the unique name (just like www.gnome.org gets routed to an IP address). However you can also own your own name, which would get routed to the same unique name, but might feel fancier.
If you look in D-Spy you will see that many object paths and interfaces are exported at org.gnome.Shell, and the same object paths and interfaces can be found under the name org.gnome.Mutter and many other names that process owns.
If you just want that run like a CLI program, you can call loop.quit() from the callback
I’m not sure what your output looks like, but assuming the service returns something created by e.g. JSON.stringify(), that should be enough. console.log() and friends are for program development etc, otherwise print() and printerr() are the easiest ways to write to stdout/stderr respectively and shouldn’t have any extra bits.