How can I run a kwin script from the command line?

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

Update: this is what I had to do in 2019 to get this to work:

# don't use ~/ in this path:
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.Scripting.run

Tags:

Kde

Plasma

Kwin