How to write a Gio.Application to make dbus calls asynchronously


This is related to Print From

Upon further research i found that:

If you are writing an application using Gio.Application … a main loop will be started for you when you call '`.

Moreover, Gio.Application has command-line(application, command_line) function as can be found in through which i can access Gio.ApplicationCommandLine which has get_stdin(), get_arguments(), get_exit_status() etc.

I think this is what I want to do.

So, I used example in Building a GTK Application | GNOME JavaScript and came up with:

#!/usr/bin/env gjs

const { GLib, Gio } =;

const connection = Gio.DBus.session;

const application = new Gio.Application({
    application_id: null,
    flags: Gio.ApplicationFlags.FLAGS_NONE

application.connect('activate', app => {
            (connection, res) => {
                try {
                    const reply = connection.call_finish(res);
                    const [string] = reply.recursiveUnpack();
                } catch (e) {
                    if (e instanceof Gio.DBusError)

                } finally {
                    // so it definitely quits



However, it does not yield any results.

What am i doing wrong here?

Gio.Application quits automatically when it is not in use. For example Gtk.Application (a subclass for graphical applications) is “in use” while there are open windows associated with it.

You can mark the app explicitly as “in-use” by calling application.hold() before the D-Bus call, and application.release() from the result callback.

You shouldn’t use Gio.Application with setting the application-id property. It’s necessary for certain characteristics to work (uniqueness, automatic resources, .desktop file matching in GNOME, …). Those don’t matter here (probably - you haven’t told us what exactly you are trying to do), but best not develop a habit that can bite you later.

1 Like

One change is i put application.release(); in finally.

Now it is working great.

Thank you very much.

I am all in with gnome. Just recently I have completed a small task. You can find it in Add workspace name to `rofi -show window` output · davatorium/rofi · Discussion #1869 · GitHub

The extension works fine but the dbus calls are ugly.

I am trying to make small cli tools (currently considering gjs scripts) which will make the dbus calls. For example, this script might be called get-workspaces.

An extension which will get some data which is now private (Meta, Shell, St …). And few cli tools that will make it easy to make those calls. Just to be clear, I have some functions like:


These do not use the private classes, so these are simple cli tools . I do not put a function in the extension unless it need to use global or any private libraries. I just write simple cli tools for that.

Please let me know if there is a better approach to this.

I know i am only scratching the surface but these tools help some gnome users.

For example, please check some of my answers:

There are other questions (and requirements) like these in the community, however i do not have account in those so i do not respond. However, these sort of issues are everywhere.

Another thing is x11,xdotool, wmctrl… I have done a small survey on “which x11,xdotool/wmctrl… commands i use”. I found that most of them are translatable if i use gnome extension.

This is the problem i am currently trying to solve as i go.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.