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

,

This is related to Print From Gio.DBus.session.call

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 'Gio.Application.run()`.

Moreover, Gio.Application has command-line(application, command_line) function as can be found in https://gjs-docs.gnome.org/gio20~2.0/gio.application#signal-command-line 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 } = imports.gi;

const connection = Gio.DBus.session;

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

application.connect('activate', app => {
            connection.call(
            'org.gnome.Shell',
            '/org/gnome/Shell/Extensions/GnomeUtilsWorkspaces',
            'org.gnome.Shell.Extensions.GnomeUtilsWorkspaces',
            'GetWorkspaces',
            null,
            null,
            Gio.DBusCallFlags.NONE,
            -1,
            null,
            (connection, res) => {
                try {
                    const reply = connection.call_finish(res);
                    const [string] = reply.recursiveUnpack();
                    print(string);
                } catch (e) {
                    if (e instanceof Gio.DBusError)
                        Gio.DBusError.strip_remote_error(e);

                    logError(e);
                } finally {
                    // so it definitely quits
                    application.quit();
                }

            }

        )
});

application.run(null);

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:

get_app_from_wmclass
get_apps
get_default_app_from_mime
get_icon_from_appid
get_icon_from_mime
get_icon_from_path
get_icon_from_uri
get_icon_from_wmclass
get_mime_from_path
get_mime_from_uri
get_path_from_uri
get_uri_from_path

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.