Raise or run application

I would like to implement the following functionality, which I could bind to a shortcut:

  1. if a given application is open, switch to it,
  2. 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.

What’s the recommended way to do this?

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.

1 Like

My main problem is actually the other part. I am happy to grep on the output of ps or similar.

But supposing I have obtained a PID, how can I raise the corresponding window in Plasma from the command line (or a script)?

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).

This has also been mentioned in KWin Scripting not exposing signals as documented (eg. clientAdded) and reported in bug 485718. So far I haven’t seen a solution.

I have 3 conky versions - so I need a script to check, kill, start…

#!/bin/bash

if pgrep -x "conky"
        then killall conky
    else 
        conky -d -c ~/.conky/c0-time.conky
        conky -d -c ~/.conky/c0-date.conky
        conky -d -c ~/.conky/c2-network.conky
        conky -d -c ~/.conky/c3-proc.conky
        conky -d -c ~/.conky/c1-information.conky
fi

Dabbling on…

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.

Could this be useful? GitHub - jinliu/kdotool: xdotool-like for KDE Wayland
It’s like xdotool but for kwin.

Ok, I managed to fix my final issues.

What follows is me documenting what was still needed to get this sorted for others (and my future self :wink: ) that may also run into this.

KWin’s dbus api has a function to dynamically load a kwin script which can then be run via a dbus api as well.

To load the script you’d run something similar to this:

dbus-send --session --dest=org.kde.KWin --print-reply=literal /Scripting org.kde.kwin.Scripting.loadScript "string:<path-to-script>" "string:<some-label>"

This will return a number that uniquely identifies the script, the script-id.

In KWin5 that script-id is also the access point for the script on the dbus api. So to run the script you’d then call something like this:

dbus-send --session --dest=org.kde.KWin --print-reply=literal "/<script-id>" org.kde.kwin.Script.run

In KWin6 however the script’s access point on the dbus api becomes /Scripting/Script<script-id>

So the above command to run a script for KWin6 becomes:

dbus-send --session --dest=org.kde.KWin --print-reply=literal "/Scripting/Script<script-id>" org.kde.kwin.Script.run

Once I updated my dbus-send commands, things were working again.

2 Likes

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:

  1. go to the pull request
  2. click on the “Files changed” tab
  3. it will show the new file in “diff” format
  4. on the top-right of this diff format, click on the three dots to reveal a menu and select “View File”
  5. that will open the file, but still embedded in the github interface.
  6. 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.
1 Like