Skip to content

Instantly share code, notes, and snippets.

@Zren
Created February 9, 2018 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zren/ae88920d235ecf583bc31972d09a69d4 to your computer and use it in GitHub Desktop.
Save Zren/ae88920d235ecf583bc31972d09a69d4 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import sys, os
def debug(*args):
# if True:
if False:
print(*args)
if len(sys.argv) >= 2:
scriptPath=sys.argv[1]
else:
scriptPath='~/.local/share/plasmashell/interactiveconsoleautosave.js'
scriptPath=os.path.abspath(os.path.expanduser(scriptPath))
debug('scriptPath', scriptPath)
# https://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html
import dbus
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
# https://github.com/KDE/plasma-workspace/blob/master/components/shellprivate/interactiveconsole/interactiveconsole.cpp
scriptingObj = bus.get_object('org.kde.KWin', '/Scripting')
scriptManager = dbus.Interface(scriptingObj, dbus_interface='org.kde.kwin.Scripting')
scriptId = scriptManager.loadScript(scriptPath)
debug('scriptId', scriptId)
scriptObj = bus.get_object('org.kde.KWin', '/{}'.format(scriptId))
script = dbus.Interface(scriptObj, dbus_interface='org.kde.kwin.Scripting')
def onPrintError(text):
print('Error: {}'.format(text))
script.connect_to_signal('printError', onPrintError)
def onPrint(text):
print(text)
script.connect_to_signal('print', onPrint)
from gi.repository import GLib
loop = GLib.MainLoop()
script.run()
try:
loop.run()
except: # Eg: KeyboardInterupt. Doesn't work for SIGTERM though... oh well.
script.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment