13

I wrote a really basic kwin script to tile windows how I like them,

// Main reference: http://techbase.kde.org/Development/Tutorials/KWin/Scripting

// Top-level parameters. Adjust these as suitable for your desktop.
var width = 3840;
var third = Math.round(width / 3);


var clients = workspace.clientList();
for (var i=0; i<clients.length; i++) {
  var client = clients[i];
  var cap = client.caption.toLowerCase();
  var g = client.geometry;

  if (cap.indexOf("firefox") != -1) {
    g.x = 0;
    g.width = third;
  } else if (cap.indexOf("vim") != -1) {
    g.x = third;
    g.width = third;
  } else if (cap.indexOf("konsole") != -1) {
    g.x = third * 2;
    g.width = third;
  }

  client.geometry = g;
}

using the scripting console (qdbus org.kde.plasma-desktop /MainApplication showInteractiveKWinConsole, or wm console from krunner).

I want to bind this to a hotkey.

I've only been able to find resources about building Plasma packages, which I haven't gotten to work (I have a metadata.desktop that's similar to the ones in /usr/share/kde4/apps/kwin/scripts, and similar directory contents, but it says plasmapkg(3980)/libplasma Plasma::Package::installPackage: Could not register package as service (this is not necessarily fatal): "kwin-script-tilewindows").

I'm a little tired of mucking with Plasma packaging. How can I invoke my JavaScript-based kwin script from the command line?

EDIT / Note

In case you're not familiar, it's easy to bind command to hotkeys in KDE -- just right-click the application launcher, go to "Edit Applications", add a new item, enter the shell command in the "Command" box, and then set the shortcut key in the "Advanced" tab.

gatoatigrado
  • 555
  • 1
  • 7
  • 16

2 Answers2

7

With some hints from here, I managed to get the following to work:

script=/path/to/script

num=$(dbus-send --print-reply --dest=org.kde.kwin.Scripting \
  /Scripting \
  org.kde.kwin.Scripting.loadScript \
  string:"$script" |
  awk 'END {print $2}' )

dbus-send --print-reply --dest=org.kde.kwin.Scripting \
  /$num \
  org.kde.kwin.Scripting.run
Graeme
  • 33,607
  • 8
  • 85
  • 110
  • Outdated. Getting `Error org.freedesktop.DBus.Error.ServiceUnknown: The name org.kde.kwin.Scripting was not provided by any .service files`. – Ashark Jul 16 '23 at 04:11
6

This is what I had to do in 2022 to get this to work

script="/path/to/script"

num=$(dbus-send --print-reply --dest=org.kde.KWin \
    /Scripting org.kde.kwin.Scripting.loadScript \
    string:"$script" | awk 'END {print $2}' )

dbus-send --print-reply --dest=org.kde.KWin /$num \
    org.kde.kwin.Script.run

# I'm not sure if some delay is in order here to let your script complete?
dbus-send --print-reply --dest=org.kde.KWin /$num \
    org.kde.kwin.Script.stop

Note: in 2019 I needed to use org.kde.kwin.Scripting.run instead of org.kde.kwin.Script.run.

I'm not clear if the "stop" commands are needed, but they seem to clean up the dbus tree, so perhaps they should be used. I'm not sure if there are negative implications to using them, so if they cause trouble, perhaps try without.

Ashark
  • 767
  • 7
  • 24
user272901
  • 71
  • 1
  • 4
  • But I cannot see the actual prints from the script. In script I wrote `print("Hello world")`. The output of last command is `method return time=1652908699.636159 sender=:1.343 -> destination=:1.556 serial=2148 reply_serial=2`. What I am doing wrong? – Ashark May 18 '22 at 21:21
  • Thanks -- updated. Yes, they seem to have changed it to "Script". I also added "stop" commands because otherwise the /1, /2, /3... get left behind. I don't know enough about this to know if that's a good idea, but I added them to my answer. – user272901 May 19 '22 at 02:01
  • 1
    As a workaround, I can read journalctl, the kwin_wayland command writes there. Here https://bugs.kde.org/show_bug.cgi?id=445058 it is described how to do it. – Ashark Jun 16 '22 at 16:27
  • 1
    The python variant is available in my repo: https://github.com/Ashark/Ashark-bin/blob/master/get_list_of_windows – Ashark Jun 16 '22 at 18:18
  • 1
    Probably, also need to run org.kde.kwin.Scripting.unloadScript, like here: https://gist.github.com/academo/613c8e2caf970fabd260cfd12820bde3#file-ww-L110 (was linked here: https://unix.stackexchange.com/a/658994/264065) Also, probably it is possible to avoid using script file: https://unix.stackexchange.com/a/413873/264065 – Ashark Jun 16 '22 at 21:58
  • @Ashark plasma scripts are not the same a Kwin Scripts. I could not find a way to run a kwin script without using a file. If you want to avoid touching the disk maybe using `/dev/shm/` is a good option? – academo Jul 17 '23 at 14:21