Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/python3
import argparse
import dbus
import subprocess
parser = argparse.ArgumentParser()
parser.add_argument("widget", type=str, help="widget namespace eg: 'org.kde.plasma.digitalclock'")
parser.add_argument("key", type=str, help="config key to modify")
parser.add_argument("value", type=str, help="new value to store in config key")
parser.add_argument("--group", type=str, help="config group (use only when required)")
args = parser.parse_args()
widgetType = args.widget or ""
configGroup = args.group or ""
configKey = args.key or ""
configValue = args.value or ""
# print("widgetType: {}".format(widgetType))
# print("configGroup: {}".format(configGroup))
# print("configKey: {}".format(configKey))
# print("configValue: {}".format(configValue))
# https://userbase.kde.org/KDE_System_Administration/PlasmaDesktopScripting
plasmaScript = """
function forEachWidgetInContainment(containmentList, callback) {
for (var containmentIndex = 0; containmentIndex < containmentList.length; containmentIndex++) {
var containment = containmentList[containmentIndex];
var widgets = containment.widgets();
for (var widgetIndex = 0; widgetIndex < widgets.length; widgetIndex++) {
var widget = widgets[widgetIndex];
callback(widget, containment);
}
}
};
function forEachWidget(callback) {
forEachWidgetInContainment(desktops(), callback);
forEachWidgetInContainment(panels(), callback);
}
function forEachWidgetByType(type, callback) {
forEachWidget(function(widget, containment) {
if (widget.type == type) {
callback(widget, containment);
}
});
}
function widgetSetProperty(args) {
if (!(args.widgetType && args.configKey)) {
return;
}
forEachWidgetByType(args.widgetType, function(widget){
var configGroups = widget.configGroups.slice();
for (var groupIndex = 0; groupIndex < configGroups.length; groupIndex++) {
var configGroup = configGroups[groupIndex];
widget.currentConfigGroup = [configGroup];
if (args.configGroup && args.configGroup != configGroup) {
continue;
}
for (var keyIndex = 0; keyIndex < widget.configKeys.length; keyIndex++) {
var configKey = widget.configKeys[keyIndex];
if (args.configKey != configKey) {
continue;
}
var configValue = widget.readConfig(configKey);
var valueType = typeof configValue;
widget.writeConfig(configKey, args.configValue);
configValue = widget.readConfig(configKey);
valueType = typeof configValue;
}
}
});
}
var args = {
widgetType: "{{widgetType}}",
configGroup: "{{configGroup}}",
configKey: "{{configKey}}",
configValue: "{{configValue}}",
}
widgetSetProperty(args);
"""
plasmaScript = plasmaScript.replace('\n', ' ')
plasmaScript = plasmaScript.replace("{{widgetType}}", widgetType)
plasmaScript = plasmaScript.replace("{{configGroup}}", configGroup)
plasmaScript = plasmaScript.replace("{{configKey}}", configKey)
plasmaScript = plasmaScript.replace("{{configValue}}", configValue)
# print(plasmaScript)
# https://dbus.freedesktop.org/doc/dbus-python/tutorial.html
# qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript ""
session_bus = dbus.SessionBus()
plasmashell_obj = session_bus.get_object('org.kde.plasmashell', '/PlasmaShell')
plasmashell = dbus.Interface(plasmashell_obj, dbus_interface='org.kde.PlasmaShell')
plasmashell.evaluateScript(plasmaScript)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.