How To Automatically Change GNOME Background In Intervals Using BASH
Table of Contents
Have you ever wanted to have that automatic background switching the feature on your GNOME Linux distro? I missed that feature after I switched from Cinnamon to GNOME 🙁 Searched for apps in the software center and alas there is none that I could find. However, today Iâm happy to let you know that there is a workaround to this missing feature through the use of BASH scripting language.
Requirement
Not much, all you need is a text editor to edit some lines of code on the script file. And the ability to create and save text files. Thatâs it 🙂 In case you are a programmer, Iâve left out comments (documented the code) in the script so you can understand it.
Step 1: Create the script file
Open your favorite text editor program and save the file as .change_wallpapers on your home directory.
Notice the dot (.) prefixed to the file name. We want that file hidden so itâs important to prefix it with a dot before the file name.\
Then copy & paste the below script to your newly created script file.
#!/bin/bash # script to set random background wallpapers on my GNOME desktop # set base path export wallpaper_path= shopt -s nullglob # store all the image file names in wallpapers array wallpapers=( $wallpaper_path/*.jpg $wallpaper_path/*.jpeg $wallpaper_path/*.png $wallpaper_path/*.bmp $wallpaper_path/*.svg ) # get array size wallpapers_size=${#wallpapers[*]}
Step 2: Modifying the script
âYou have to enter your own custom path on the environment variable wallpaper_path. For instance, if your wallpaper (or picture) directory is in /home/abc/Pictures/Wallpapers you would have to modify that line:export wallpaper_path=<your wallpaper folder path>to
export wallpaper_path=/home/abc/Pictures/WallpapersMoreover, Iâve added the most common image file extensions on the array variable wallpapers. But your preferred image file format might have been missed out. Maybe your images are in the format TIFF or something else. Make sure you add those missing extensions yourself. For instance, if I want to add support for tif, Iâll have to add this statement just below
$wallpaper_path/*.svg line as in: $wallpaper_path/*.svg $wallpaper_path/*.tif
Step 3: Choose one of the below two options
Choose only one of them… and skip the other. Otherwise, youâll run into issues.
â
The first option is if you want your wallpaper to change in sequential order ie., starting from the first image and continuing to the last. And then repeat the same process over and over. So you are not stuck with the last image when the loop runs out. Copy & paste the below code on the last line of your script file.
Change background in sequential order
# set wallpapers in incremental order index=0 while [ $index -lt $wallpapers_size ] do gsettings set org.gnome.desktop.background picture-uri ${wallpapers[$index]} # index is maxing out, so reset it if [ $(($index+1)) -eq $wallpapers_size ] then index=0 else index=$(($index + 1)) fi # keep the wallpaper for the specified time sleep 15m done
Change background in random order
If you prefer randomization over sequential order where your wallpapers are displayed randomly. Copy & paste the below code on the last line of your script file.
# set random wallpapers # loop infinitely while true do # generate random index random_index=$(($RANDOM % $wallpapers_size)) # then set a random wallpaper gsettings set org.gnome.desktop.background picture-uri ${wallpapers[$random_index]} # keep the wallpaper for the specified time sleep 15m done
Step 4: Finishing up
Your .change_wallpapers script is done. Now there is only one thing left to do; starting that script when you log in.
For that, we have to append some code on your .profile file to start that script. If you still you have your text editor opened, launch your .profile file and copy these four lines of code on the last line.
# start my custom script for setting random background wallpapers if [ -f "$HOME/.change_wallpapers" ] ; then bash $HOME/.change_wallpapers & fi
In case you couldnât find your hidden file .profile on the open dialog box, press CTRL + H to show all the hidden files.
Later, you can press the same key combination to disable showing hidden files and folders from your file manager program.
After you are done, either reboot or re-login your session for the script to come into effect.
Conclusion
I hope you have found this tutorial simple and easy to follow. And successfully got your script working 🙂 Let me know your experience in the comment section below. By the way, if you ever want to disable this automatic background switching script in the future, all you have to do is delete that .change_wallpapers script file and remove those additional four lines on your .profile file.
LinuxAndUbuntu Newsletter
Join the newsletter to receive the latest updates in your inbox.