
Gnome 扩展可命令行列出 Wayland 当前窗口
来源标题:Gnome Extension for getting windows list in Wayland
一款 Gnome 扩展支持从命令行列出当前窗口及其属性,并可将指定窗口移动到其他工作区。该扩展对 Wayland 环境尤其有用,能获取当前聚焦窗口信息,输出包含窗口类名、PID、位置、尺寸和工作区等字段。用户可通过 gnome extensions page 安装后使用 dbus-send 或 gdbus 调用。
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
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.
Installation
Install extension from gnome extensions page.
To get all active windows simply run from terminal:
dbus-send --session --print-reply=literal
--dest=org.gnome.Shell
/org/gnome/Shell/Extensions/Windows
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, "workspace": 0, "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:
Both methods should be invoked giving desired window's id as a parameter. Example usages:
dbus-send --session --print-reply=literal
--dest=org.gnome.Shell
/org/gnome/Shell/Extensions/Windows
org.gnome.Shell.Extensions.Windows.Details uint32:821316218
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:
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 7 methods providing such functionality:
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, --print-reply=literal is needed.
For example, To view all windows in json:
dbus-send --session --print-reply=literal
--dest=org.gnome.Shell
/org/gnome/Shell/Extensions/Windows
org.gnome.Shell.Extensions.Windows.List
| jq .
To get windowID of all windows in current workspace:
dbus-send --session --print-reply=literal
--dest=org.gnome.Shell
/org/gnome/Shell/Extensions/Windows
org.gnome.Shell.Extensions.Windows.List
| jq -c '.[] | select (.in_current_workspace == true) | .id'
To get windowID and wm_class of all windows in current workspace:
dbus-send --session --print-reply=literal
--dest=org.gnome.Shell
/org/gnome/Shell/Extensions/Windows
org.gnome.Shell.Extensions.Windows.List
| jq -c '[.[] | select (.in_current_workspace == true) | {id: .id,wm_class: .wm_class}]'
To get windowID and wm_class of all visible window:
dbus-send --session --print-reply=literal
--dest=org.gnome.Shell
/org/gnome/Shell/Extensions/Windows
org.gnome.Shell.Extensions.Windows.List
| 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:
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。