I would like to implement the following functionality, which I could bind to a shortcut:
if a given application is open, switch to it,
if it is not open, start it
A tool that this this on X11 is jumpapp, but it does not support Wayland. I found a script that is supposed to do that (ww-run-raise), but it does not work for KDE 6.
So, the main problem is detecting if an application is “open” - unlike with X11, under Wayland one application cannot detect what other applications are running - its a security thing. You can either write a KWin script that will be able to do that because it runs inside KWin (see this KDE scripting tutorial to try to tackle that), or you can list the system processes using ps or something like that and try to figure out if the application that you are interested in has an active process.
It’s basically the same problem - you can’t know a window other than your own unless you get cooperation from KWin. In a KWin script you can run workspace.activateClient(client, true, false, false) to activate the window if you found its client object. If you know an application’s process ID, you can list all the clients from the KWin workspace and check their pid property until you find the correct one.
ww-run-raise more or less solved that issue, exactly by using kwin scripts. However since kde 6 it seems the ‘workspace’ variable is no longer exposed to scripts, making it rather difficult to get window information from kwin (unless I’m missing something).
I made some progress for my specific script. When I replace workspace.clientList() with workspace.windowList() and workspace.activeClient with workspace.activeWindow the script works when invoked from the plasma-interactiveconsole.
In kde 5 I handling the kwin scripts via dbus-send commands, similar to what ww-run-raise does. But for some reason this is still not working for my adjusted kwin scripts. Perhaps the dbus interface has changed as well, but I have not found any hints so far.
I found that there is a pull request against ww-run-raise to add a version that’s compatible with KWin6. It’s not merged yet, but the pull request is really adding a second script with a different name. You could download that new file (at the time of this writing, it is found at this link).
Should that link no longer be valid at some point, I got it via these steps:
on the top-right of this diff format, click on the three dots to reveal a menu and select “View File”
that will open the file, but still embedded in the github interface.
on the top right of this file you have 3 buttons to get the raw file. The first is to view it in the browser, the second is to copy a link and the third is to directly download it.