Yes, it is possible. The idea is to ask kwin for this information. It is done via kwin script. It can only communicate the world with dbus, so we cannot run shell commands in kwin scripts (at least directly). But we can run kwin script from shell script.
Create the following script ~/bin/list_windows.js:
const clients = workspace.clientList();
for (var i = 0; i < clients.length; i++) {
print(clients[i].caption);
}
Unfortunately, the output to stdout is currently broken, see bug report. But there is a workaround. Turn on the kde systemd startup. Now we can use journalctl to pull the output. The resulting get_list_of_windows script is the following:
#!/usr/bin/env python3
import subprocess
from datetime import datetime
def get_list_of_windows():
datetime_now = datetime.now()
script = "/home/andrew/bin/list_windows.js"
reg_script_number = subprocess.run("dbus-send --print-reply --dest=org.kde.KWin \
/Scripting org.kde.kwin.Scripting.loadScript \
string:" + script + " | awk 'END {print $2}'",
capture_output=True, shell=True).stdout.decode().split("\n")[0]
subprocess.run("dbus-send --print-reply --dest=org.kde.KWin /" + reg_script_number + " org.kde.kwin.Script.run",
shell=True, stdout=subprocess.DEVNULL)
subprocess.run("dbus-send --print-reply --dest=org.kde.KWin /" + reg_script_number + " org.kde.kwin.Script.stop",
shell=True, stdout=subprocess.DEVNULL) # unregister number
since = str(datetime_now)
msg = subprocess.run("journalctl _COMM=kwin_wayland -o cat --since \"" + since + "\"",
capture_output=True, shell=True).stdout.decode().rstrip().split("\n")
msg = [el.lstrip("js: ") for el in msg]
return msg
print('\n'.join(get_list_of_windows()))
Now run the script and you will get the output:
$ get_list_of_windows
Рабочий стол по умолчанию — Plasma
Plasma
Is there a way to get list of windows on KDE Wayland? - Unix & Linux Stack Exchange - Vivaldi
get_list_of_windows — Kate
Andrew Shark / Davinci Resolve Scripts · GitLab — Falkon
project1 – README.md
This is in my repo.