How to Get Get Window's Width, Height, X-Coordinate, Y-Coordinate, ActiveWorkSpace and IsViewable

I have checked DevDocs

I can get

[
  {
    "class": "Nemo-desktop",
    "class_instance": "nemo-desktop",
    "pid": 1949,
    "id": 3315575505,
    "maximized": 0,
    "focus": false,
    "title": "Desktop"
  },
]

Using

let win = global.get_window_actors()
    .map(a => a.meta_window)
    .map(w => ({ class: w.get_wm_class(), class_instance: w.get_wm_class_instance(), pid: w.get_pid(), id: w.get_id(), maximized: w.get_maximized(), focus: w.has_focus(), title: w.get_title()}));
return JSON.stringify(win);

I also want to get Width, Height, X-Coordinate, Y-Coordinate, ActiveWorkSpace and IsViewable for each window.

Here, ActiveWorkSpace is the workspace on which the window is.

Here, IsViewable is the value shown with in xwininfo's output’s Map State: IsViewable. This is same as xdotools's --onlyvisible

One pore issue with the code. The output is not in json format.

The output is actually like:

('[
  {
    "class": "Nemo-desktop",
    "class_instance": "nemo-desktop",
    "pid": 1949,
    "id": 3315575505,
    "maximized": 0,
    "focus": false,
    "title": "Desktop"
  },
]',)

So I have to run gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.List | cut -c 3- | rev | cut -c4- | rev | jq . to get accurate output. I would like to have a json output.

How can i achieve these goals.

I think most of it is working (except IsViewable). I am not an expert (in neither javascript not gnome) in any shape or form. Please check the code and let me know whether i am moving in the right direction or not.

const { Gio } = imports.gi;

const MR_DBUS_IFACE = `
<node>
    <interface name="org.gnome.Shell.Extensions.Windows">
        <method name="ListWindows">
            <arg type="s" direction="out" name="winJsonArr"/>
        </method>
    </interface>
</node>`;


class Extension {
    enable() {
        this._dbus = Gio.DBusExportedObject.wrapJSObject(MR_DBUS_IFACE, this);
        this._dbus.export(Gio.DBus.session, '/org/gnome/Shell/Extensions/Windows');
    }

    disable() {
        this._dbus.flush();
        this._dbus.unexport();
        delete this._dbus;
    }
    ListWindows() {
        let win = global.get_window_actors();

        let workspaceManager = global.workspace_manager;
        //let activeWorkspace = workspaceManager.get_active_workspace_index();


        var winJsonArr = [];
        win.forEach(function (w) {
            winJsonArr.push({
                class: w.meta_window.get_wm_class(),
                class_instance: w.meta_window.get_wm_class_instance(),
                pid: w.meta_window.get_pid(),
                id: w.meta_window.get_id(),
                width: w.get_width(),
                height: w.get_height(),
                x: w.get_x(),
                y: w.get_y(),
                maximized: w.meta_window.get_maximized(),
                focus: w.meta_window.has_focus(),
                title: w.meta_window.get_title(),
                workspace: w.meta_window.located_on_workspace(workspaceManager.get_active_workspace())
            });
        })

        return JSON.stringify(winJsonArr);
    }
}

function init() {
    return new Extension();
}

You have to run gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.ListWindows | cut -c 3- | rev | cut -c4- | rev | jq . to get the output. Could not get in pure json yet.

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