Automating KDE Plasma 5 from a shell (qdbus, global shortcuts, taskbar pins, idle inhibit)
kdeplasmaqdbusdbuskglobalaccelgtk4screensaverx11
KDE Plasma 5.27 exposes almost everything over the session D-Bus, so a non-GUI shell (for example a Claude session) can pin apps, register global shortcuts, and inhibit the screen locker without touching the GUI. This article collects the recipes that were proven on VictusPop (Plasma 5.27.12, X11).
Reaching the session from a plain shell
- D-Bus:
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus, thenqdbus(Qt5) works. - X11 apps:
DISPLAY=:N app &. The display number is not fixed (it moves after an X crash); read it fromps -C Xorg -o args=. Wayland-only launches fail from such a shell because there is no Wayland socket. See Driving a desktop from a headless agent shell (DISPLAY, clipboard, sudo hand-off). pkexecshows a graphical password prompt throughpolkit-kde-authentication-agent-1, so GUI-launched scripts can get root even whensudocannot be typed.
Pin an app to the taskbar
Plasma scripting via qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript '<js>': iterate panels(), find widgets of type org.kde.plasma.icontasks, append applications:<name>.desktop to the launchers config in group General, then reloadConfig(). Takes effect live.
Register a global shortcut for a .desktop launcher
- Put
X-KDE-Shortcuts=Ctrl+Alt+Qin~/.local/share/applications/<app>.desktop; runkbuildsycoca5. systemctl --user stop plasma-kglobalaccel.service(stop it first or the daemon overwrites your edit).kwriteconfig5 --file kglobalshortcutsrc --group <app>.desktop --key _launch "Ctrl+Alt+Q,none,<Friendly Name>"(plus_k_friendly_name).systemctl --user start plasma-kglobalaccel.service.
KDE grabs the combo before the app sees it, so to make the same combo also exit the app, make the second launch quit the running instance (GtkApplication: do_activate calls self.quit() when already active). Ctrl+Alt+Del is taken by KDE's "Log Out".
Inhibit the idle screen locker
Call org.freedesktop.ScreenSaver.Inhibit on /ScreenSaver and release in shutdown. The hold shows in PowerDevil's PolicyAgent.ListInhibitions only after about 5 s (PowerDevil's registration delay; checking earlier looks like a failure). Lock timing lives in ~/.config/kscreenlockerrc ([Daemon] Timeout=, LockGrace=). If you hold off the lock for hours, consider calling ScreenSaver.Lock on exit so the machine is not left unlocked.
Example: the screen-off tool (~/.local/bin/screen-off, Python + GTK4) shows an undecorated fullscreen black window per monitor, hides the cursor, swallows input, toggles on Ctrl+Alt+Q, and inhibits the locker. It exists because powering the monitor off crashes the session (Powering off an HDMI monitor kills the KDE X11 session (hybrid-GPU laptop)).
Override a system .desktop file per user
Copy it to ~/.local/share/applications/ with the same name, edit, kbuildsycoca5. Undo by deleting the copy. Note the override shadows future package updates of that file.
Example: KDE Discover (and the Updates tray icon) opened no window because plasma-discover deadlocked at startup loading its snap backend. A gdb backtrace showed a lock-order inversion: the main thread held the dynamic-loader lock in dlopen(snap-backend.so) (libsoup-3 initializer) and waited for the GModule lock, while a packagekit-backend thread held the GModule lock and waited for the loader lock in dlopen(libgvfsdbus.so). Fix without root: override org.kde.discover.desktop adding --backends packagekit-backend,fwupd-backend,kns-backend to both Exec= lines (main entry and the Updates action the tray uses). Side effect: snap updates are not listed (snapd refreshes itself anyway). Alternative: sudo apt remove plasma-discover-backend-snap.
Testing tools when xdotool is missing
python-xlib:Xlib.ext.xtest.fake_inputsynthesizes keys, including KDE global shortcuts.xwininfo -name "<title>"andxprop _NET_WM_STATEverify a window.- With
ptrace_scope=1, gdb cannot attach to a running process; launch the process under gdb instead.
Gotchas
pgrep -f pattern/pkill -f patternmatch your own shell command line if it contains the pattern; apkillkilled the calling command (exit 144). Use a bracket pattern ([s]creen-off) or check windows instead.- Do not assume GNOME or COSMIC because their binaries are installed; check which shell is actually running (
plasmashell+kwin_x11).
See also
- Driving a desktop from a headless agent shell (DISPLAY, clipboard, sudo hand-off)
- VictusPop, Justin's desktop (hardware, OS, boot, quirks)
- "Mouse and keyboard dead" on X11: frozen display vs stuck grab