This content originally appeared on DEV Community and was authored by Rob O'Leary
Having a beautiful wallpaper (desktop image) can boost your work environment aesthetic. I like the idea of swapping out the image regularly to keep things looking fresh.
Im going to write a short script to do this for me and set a cron job to run it regularly. First, I will look at doing this in Gnome/Unity, and then I will look at option that works in different desktop environments using a command-line tool instead (no scripting required).
This is what we want..
Gnome/Unity
Gnome 3 and Unity has a gsettings command-line tool to view and change user settings. If you are using an older version of a distro, you may be using Gnome 2. If that is the case, you will use gconftool instead.
User data is stored as key-value pairs. We just need to find the right keys to set.
The keys we are interested in are:
-
picture-options: The rendering method. This decides what to do with the image if it is smaller or bigger than the screen resolution. I find thatscaledworks best most of the time: it centers the image and leaves it as its natural size if it is smaller, and it downscales the image if it is bigger. -
picture-uri: The URI of the image file to display.
#!/bin/bash
folder="${HOME}/pictures/wallpapers"
pic=$(ls $folder/* | shuf -n1)
# values for picture-options: ‘none’, ‘wallpaper’, ‘centered’, ‘scaled’, ‘stretched’, ‘zoom’, ‘spanned’
gsettings set org.gnome.desktop.background picture-options scaled
gsettings set org.gnome.desktop.background picture-uri "file://$pic"
Now, we just need to set up a cron job to run the script regularly. First though, ensure your script has the execute permission set by running sudo chmod +x /home/your-name/.local/bin/change-wallpaper.
To make it work as a cron job, we need to set DBUS_SESSION_BUS_ADDRESS environment variable to ensure we can communicate with Gnome from another process. Open crontable with crontab -e and add the following lines to set-up a hourly job:
0 * * * * env DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /home/your-name/.local/bin/change-wallpaper
If you are not familiar with cron, you can use crontab.guru to help you make a different schedule.
Other Desktop Environments
While it is fairly simple for Gnome, KDE and XFCE users to change the wallpaper on their desktop. If you are using a lightweight desktop manager such Openbox or Fluxbox, you will find that there is no way that you can set the wallpaper. In this case, Nitrogen will come in handy.
Nitrogen is a simple, lightweight application that allows you to change the background of your desktop with a nice set of options.
To get the same result as the previous section, we run the command:
nitrogen --random --set-scaled ~/pictures/wallpapers/
Lets run it as a hourly cron job again. First, open crontable with crontab -e and add the following lines to set-up a hourly job:
0 * * * * env DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus nitrogen --random --set-scaled ~/pictures/wallpapers
If you are not familiar with cron, you can use crontab.guru to help you make a different schedule.
You can find both examples in this repo: https://github.com/robole/bash-scripts.
Thats it! Enjoy the virtual view!
This content originally appeared on DEV Community and was authored by Rob O'Leary
Rob O'Leary | Sciencx (2021-09-02T10:15:40+00:00) Automatically change your wallpaper on Linux. Retrieved from https://www.scien.cx/2021/09/02/automatically-change-your-wallpaper-on-linux/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.
