Setting wallpaper in a script with KDE Plasma

Fri, 30/06/2023 - 10:51 -- James Oakley
Screenshot of the desktop on KDE Plasma

I'm currently running a computer that uses Fedora Linux, with the spin that comes with KDE Plasma as the desktop (I happen to prefer that to Gnome).

I wanted a script that will download a desktop wallpaper from the internet, and then set that to be my current wallpaper.

I have searched the web for the best way to do this, and hit advice that is outdated or inaccessible. Outdated, because it's many years old and things have moved on. Inaccessible because it seems the link in Google's index to every URL in the KDE forums is a broken link (they must have restructured their forum and not set up redirects), and because the KDE Reddit r/kde is private and I can't view the pages that come up in search.

So, for the benefit of others, here's what I've found.

You don't need to use qdbus

There are lots of webpages that tell you how to write a script that calls qdbus, to run org.kde.PlasmaShell.evaluateScript. I did get those to work, but because you're basically assembling a javascript command to pass, it's more fiddly than it needs to be to make sure you pass in the correct filename. You discover things like needing a full path not a relative one. You'll get there, but you end up with 6 lines of code that you have to adjust until you get it to work.

The reason you don't need that is that you can just call /usr/bin/plasma-apply-wallpaperimage. It just does the job out of the box, and you can pass in relative paths too.

$ plasma-apply-wallpaperimage --help
Usage: plasma-apply-wallpaperimage [options] imagefile
This tool allows you to set an image as the wallpaper for the Plasma session.

 -h, --help  Displays help on commandline options.
 --help-all  Displays help including Qt specific options.

 imagefile   An image file or an installed wallpaper kpackage that you wish to
             set as the wallpaper for your Plasma session

So you literally just call

plasma-apply-wallpaperimage ~/wallpaper/my-wallpaper.jpg

and it works.

You need a unique filename

Or, at least, it nearly works.

If you run that command once, it will indeed change your wallpaper.

If you download another picture to my-wallpaper.jpg, and run it again, nothing changes. If you then right click on your desktop to pull up the dialog where you'd manually set the wallpaper, your new picture is there in the dialog, but nothing changes until you log out and log back in. Which I don't want to have to do.

It turns out that if you run plasma-apply-wallpaperimage with the same filename you used before, the system assumes the picture hasn't changed, so the change is not fully applied.

So all you need to do is download your filename so the filename is not identical to the one you had before. And then it will work.

 DATE=$(date +"%Y%m%d-%H%M%S")
 wget $URL -O wallpaper-$DATE.jpg
 plasma-apply-wallpaperimage wallpaper-$DATE.jpg

Blog Category: 

Add new comment

Additional Terms