So far as I can tell, changing your wallpaper (using the default wallpaper plugin, not any fancy scripted wallpaper plugins) from the command line in KDE4 is needlessly hard. I have to write a JavaScript file to a temporary location, make a dbus call to load it into an interactive window, and then use xdotool to simulate key strokes to run it. Jimminy cricket. But below is how I have it done. If there’s an easier way that I’ve missed, pleeeaassseee let me know in the comments.

set-wallpaper.sh:

#!/bin/sh
js=$(mktemp)
cat > $js <<_EOF
var wallpaper = "$1";
var activity = activities()[0];
activity.currentConfigGroup = new Array("Wallpaper", "image");
activity.writeConfig("wallpaper", wallpaper);
activity.writeConfig("userswallpaper", wallpaper);
activity.reloadConfig();
_EOF
qdbus org.kde.plasma-desktop /App local.PlasmaApp.loadScriptInInteractiveConsole "$js" > /dev/null
xdotool search --name "Desktop Shell Scripting Console – Plasma Desktop Shell" windowactivate key ctrl+e key ctrl+w
rm -f "$js"
November 18, 2011 · [Print]

10 Comments to “Set Wallpaper from Command-line in KDE4”

  1. GC says:

    A possible alternative is to set the background to “Gallery”, make it point to a directory with a single file and overwrite that file. As an alternative, you can use http://code.google.com/p/ksetwallpaper/ (even though this is simpler because it merely “already exist”).

    • Jason says:

      “A possible alternative is to set the background to “Gallery”, make it point to a directory with a single file and overwrite that file.”

      Clever, but crude, and missing the point.

      “As an alternative, you can use http://code.google.com/p/ksetwallpaper/ (even though this is simpler because it merely “already exist”).”

      One of the plugin approaches, which is kind of ‘meh’, and more importantly, I don’t want to load more python into the runtime just to occasionally have the ability to change the background. Seems to be missing the point.

      But these are nonetheless interesting, so thanks for sharing them.

  2. GC says:

    It does not work on Kubuntu 11.10.
    If started from yakuake, xdotool fails. If started from konsole, xdotool succeeds, but the wallpaper does not change. Any idea on how to debug it?

  3. Aaron Seigo says:

    the obvoius way to do it is, using the normal default “Image” plugin, simply overwrite the file that the wallpaper points to. this causes the wallpaper to be automatically updated, and takes your script down to one “cp” command. :)

    this was introduced in commit 78ed1699e8e73527d763996cdacdfc97f3557d0f to kde-workspace on Dec 15, 2010 by yours truly…so as long as you are using Plasma Workspaces >=4.6 you sould be good.

  4. Will Stephenson says:

    @aseigo don’t forget, it was ours truly (openSUSE) who prompted you to commit that because we were doing exactly that to update the default wallpaper on distro upgrade :P .

  5. Alejandro Guerra says:

    ~>$ enlightenment_remote -desktop-bg-add opt1 opt2 opt3 opt4 opt5

    opt1 = contrainer no.
    opt2 = zone no.
    opt3 = desk_x.
    opt4 = desk_y
    opt5 = file path

    Changing Wallpaper like a boss.

  6. Matthias says:

    “enlightenment_remote -desktop-bg-add opt1 opt2 opt3 opt4 opt5″

    missing the point like a boss since this is a kde4 topic not a enlightment topic.

  7. X4 says:

    Hi, I’ve made this Gist. You script doesn’t work anymore with KDE. I’m running:
    Qt: 4.8.4
    KDE: 4.10.1

    https://gist.github.com/X4/5197205
    I also had no luck changing the KDE Wallpaper, but maybe it works for you, plus it works for all other distros.

  8. Miklos says:

    The feature mentioned by Aaron Seigo was removed when a similar feature was added in 4.11. Now you have to use a single image in a slideshow, and overwrite that. See my comment at https://bugs.kde.org/show_bug.cgi?id=325380

Leave a Reply