KDE – How to Run a Kwin Script from Command Line

kdekwinplasma

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.

Best Answer

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
Related Question