/window-calls

Gnome Extension for getting windows list in wayland

Primary LanguageJavaScript

Window Calls

This extension allows you to list current windows with some of their properties from command line, super usefull for Wayland to get current focused window.

Also, it allows you to move given window to different workspace.

Credit to dceee for providing example code in this discussion and to blueray453 for requesting additional functions and providing code example for additional properties returned by List method in issue #1;

Usage

Installation

Install extension from gnome extensions page.

Listing windows

To get all active windows simply run from terminal:

gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.List

Call returns list of window properties. Example output:

[
  {
    "wm_class": "Caprine",
    "wm_class_instance": "caprine",
    "pid": 62901,
    "id": 1610090767,
    "frame_type": 0,
    "window_type": 0,
    "width": 1910,
    "height": 2100,
    "x": 10,
    "y": 50,
    "in_current_workspace": false,
    "monitor": 0
  }
]

Moving windows between workspaces

To move given window to some workspace, in my case window with id 2205525109 to workspace number 4:

gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.MoveToWorkspace 2205525109 4

Getting additional information about window

List method only returns basic information about the window. There are two more methods:

  • Details, which returns detailed information about window in JSON format
  • GetTitle, which returns windows title. Title can contain special characters, which can break ceratin tools like jq when parsing JSON
  • GetFrameBounds, which returns windows frame bounds in JSON dictionary. This stopped working around Gnome 43, so I moved this property to additional callable function

Both methods should be invoked giving desired window's id as a parameter. Example usages:

gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.Details 2205525109

Example result of calling Details:

{
  "wm_class": "Caprine",
  "wm_class_instance": "caprine",
  "pid": 62901,
  "id": 1610090767,
  "width": 1910,
  "height": 2100,
  "x": 10,
  "y": 50,
  "maximized": 0,
  "focus": false,
  "in_current_workspace": false,
  "moveable": true,
  "resizeable": true,
  "canclose": true,
  "canmaximize": true,
  "canminimize": true,
  "canshade": true,
  "display": {},
  "frame_type": 0,
  "window_type": 0,
  "layer": 2,
  "monitor": 0,
  "role": "browser-window",
  "area": {},
  "area_all": {},
  "area_cust": {}
}

Resizing and moving

Resizing and moving can be done either together or separetely. There are 3 methods providing this functionality:

  1. Resize which takes 3 parameters: winid width height
  2. MoveResize which takes 3 parameters: winid x y width height
  3. Move which takes 3 parameters: winid x y

Negative parameter values

When calling Move or MoveResize you sometimes want to pass negative x or y value. In order to do so, you need to add ~~ before arguments in gdbus call like so:

gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.Move ~~ 12345678908 -32 -13

Maximizing, minimizing, activating, closing

Ther are 6 methods providing such functionality:

  1. Maximize
  2. Minimize
  3. Unmaximize
  4. Unminimize
  5. Activate
  6. Close

Each takes only one parameter, winid.

Using With jq

You can realize the full power of this extension when you use it with jq or similar tool. As gdbus call returns its own structure, which is not JSON parsable, you need to use cut or gawk in order to retrieve pure JSON output from calls.

For example, To view all windows in json:

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 windowID of all windows in current workspace:

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 -c '.[] | select (.in_current_workspace == true) | .id'

To get windowID and wm_class of all windows in current workspace:

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 -c '[.[] | select (.in_current_workspace == true) | {id: .id,wm_class: .wm_class}]'

To get windowID and wm_class of all visible window:

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 -c '[.[] | select (.frame_type == 0 and .window_type == 0) | {id: .id,wm_class: .wm_class}]'

To get windowID and wm_class of all visible window in current workspace:

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 -c '[.[] | select (.in_current_workspace == true and .frame_type == 0 and .window_type == 0) | {id: .id,wm_class: .wm_class}]' | jq .

Calls using gawk

You can also use gawk to capture desired JSON values. It has to be paired with sed in order to replace escaping done by qawk on quotes. For List gawk should look for JSON list:

dbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.List | gawk 'match($0, /\[.*\]/, a) {print a[0]}' | sed 's/\\"/"/g' | jq .

And for Details you want to find just one dictionary:

gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.Details 1610090767 | gawk 'match($0, /\{.*\}/, a) {print a[0]}' | sed 's/\\"/"/g' | jq .