Ash*_*ark 5 kde x11 kwin wayland wmctrl
在 X11 时代,我可以wmctrl -l
列出可用的窗口,以便在我的脚本中使用。
$ wmctrl -l\n0x01000050 0 my-pc project1 \xe2\x80\x93 Readme.md\n
Run Code Online (Sandbox Code Playgroud)\n但现在大多数应用程序都使用 Wayland。上面的命令仅显示使用 XWayland 运行的窗口。
\n我希望能够在 Wayland 模式下使用应用程序,同时能够为我的脚本列出它们的窗口。那可能吗?我正在使用 Arch Linux 和 KDE。
\n对的,这是可能的。这个想法是向 kwin 询问此信息。它是通过 kwin 脚本完成的。它只能与 dbus 进行通信,因此我们无法在 kwin 脚本中运行 shell 命令(至少不能直接运行)。但我们可以从 shell 脚本运行kwin 脚本。
\n创建以下脚本~/bin/list_windows.js
:
const clients = workspace.clientList();\nfor (var i = 0; i < clients.length; i++) {\n print(clients[i].caption);\n}\n
Run Code Online (Sandbox Code Playgroud)\n不幸的是,标准输出的输出当前已损坏,请参阅错误报告。但有一个解决方法。打开kde systemd 启动。现在我们可以使用journalctl来提取输出。生成的get_list_of_windows
脚本如下:
#!/usr/bin/env python3\n\nimport subprocess\nfrom datetime import datetime\n\n\ndef get_list_of_windows():\n datetime_now = datetime.now()\n\n script = "/home/andrew/bin/list_windows.js"\n\n reg_script_number = subprocess.run("dbus-send --print-reply --dest=org.kde.KWin \\\n /Scripting org.kde.kwin.Scripting.loadScript \\\n string:" + script + " | awk \'END {print $2}\'",\n capture_output=True, shell=True).stdout.decode().split("\\n")[0]\n\n subprocess.run("dbus-send --print-reply --dest=org.kde.KWin /" + reg_script_number + " org.kde.kwin.Script.run",\n shell=True, stdout=subprocess.DEVNULL)\n subprocess.run("dbus-send --print-reply --dest=org.kde.KWin /" + reg_script_number + " org.kde.kwin.Script.stop",\n shell=True, stdout=subprocess.DEVNULL) # unregister number\n\n since = str(datetime_now)\n\n msg = subprocess.run("journalctl _COMM=kwin_wayland -o cat --since \\"" + since + "\\"",\n capture_output=True, shell=True).stdout.decode().rstrip().split("\\n")\n msg = [el.lstrip("js: ") for el in msg]\n\n return msg\n\n\nprint(\'\\n\'.join(get_list_of_windows()))\n\n
Run Code Online (Sandbox Code Playgroud)\n现在运行脚本,您将得到输出:
\n$ get_list_of_windows\n\xd0\xa0\xd0\xb0\xd0\xb1\xd0\xbe\xd1\x87\xd0\xb8\xd0\xb9 \xd1\x81\xd1\x82\xd0\xbe\xd0\xbb \xd0\xbf\xd0\xbe \xd1\x83\xd0\xbc\xd0\xbe\xd0\xbb\xd1\x87\xd0\xb0\xd0\xbd\xd0\xb8\xd1\x8e \xe2\x80\x94 Plasma\nPlasma\nIs there a way to get list of windows on KDE Wayland? - Unix & Linux Stack Exchange - Vivaldi\nget_list_of_windows \xe2\x80\x94 Kate\nAndrew Shark / Davinci Resolve Scripts \xc2\xb7 GitLab \xe2\x80\x94 Falkon\nproject1 \xe2\x80\x93 README.md\n
Run Code Online (Sandbox Code Playgroud)\n这是在我的仓库中。
\n