KDE: Change Wallpaper Based on Session Type
I like having interesting, beautiful wallpapers (desktop backgrounds) on my computer. For example, I enjoy Simon Stalenhag's retro-future illustrations:

But when I log in remotely, over a sometimes slow connection, it would be better to have my wallpaper just be a solid color. Solid colors compress better and therefore take up less bandwidth. Something like this:

But there's a problem: I could switch the wallpaper every time I log in to whichever is appropriate (image when I'm at my desk, solid color when I'm traveling), but I'm lazy.
One of the quirks of my set up is that when I'm at my computer I log in to a Wayland session, but when I'm remote I log in to an X11 (Xorgrdp) session. I figured maybe I could change the wallpaper based on the session type, but that's not a feature that's built in to KDE. I asked around on the #kde-offtopic IRC channel, and a person named sig` gave me a link to a script for a starting point. After poking around a bit, I rewrote it to do exactly what I want.
I set the script up to auto-start on login (System Settings > Autostart) and that was it! When I login
It's pretty short, so here it is in its entirety (you can also download it here).
# qdbus binary name varies by distro
QDBUS=$(command -v qdbus6 || command -v qdbus-qt6 || command -v qdbus)
# wait for plasmashell to hit the bus (avoids a login race)
for i in $(seq 1 30); do
"$QDBUS" org.kde.plasmashell /PlasmaShell >/dev/null 2>&1 && break
sleep 1
done
if [ "$XDG_SESSION_TYPE" = "x11" ]; then
plugin="color"
else
plugin="image"
fi
"$QDBUS" org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript '
var plugin = "'$plugin'";
apply_color = function(desktop) {
if (desktop.wallpaperPlugin != "org.kde.color") {
// This if guard is used because I found that running this readConfig/writeConfig
// code makes the color change to black if the wallpaper plugin is already
// set to color. Maybe a bug in KDE?
desktop.wallpaperPlugin = "org.kde.color";
desktop.currentConfigGroup = Array("Wallpaper", "org.kde.color", "General");
var c = desktop.readConfig("Color");
desktop.writeConfig("Color", c);
}
}
apply_image = function(desktop) {
desktop.wallpaperPlugin = "org.kde.image";
desktop.currentConfigGroup = Array("Wallpaper", "org.kde.image", "General");
var img = desktop.readConfig("Image");
desktop.writeConfig("Image", img);
}
var d = desktops();
for (i = 0; i < d.length; i++) {
if (plugin == "color") {
apply_color(d[i]);
}
else {
apply_image(d[i]);
}
}
'
When it runs, it checks if you're in an X11 session, and if you are, then it sets the wallpaper to whatever the previous color was. If you're in a Wayland session, it sets the wallpaper to whatever the previous image was.
If this is helpful to you, please let me know!
Comments