This script allows to change randomly the wall-paper on a Raspberry Pi with Desktop. Using a CRON task, the change can occur at times defined by the User.
#!/bin/bash
#################################################
## A random wallpaper changer for Raspberry Pi ##
#################################################
## Settings - Put your dir to your wallpaper folder here.. (like: /home/pi/Pictures/Wallpapers)
dir='/home/pi/Pictures'
# The three following lines are mandatory. Without them, the wallpaper will not change
# as the DISPLAY is not accessible froma CRON job.
export DISPLAY=:0
export XAUTHORITY=/home/pi/.Xauthority
export XDG_RUNTIME_DIR=/run/user/1000
## Step one - Random File selection
file=`/bin/ls -1 "$dir" | sort --random-sort | head -1`
path=`readlink --canonicalize "$dir/$file"`
## Step two - pushing result to pcmanfm (Which manages the wallpaper)
pcmanfm --set-wallpaper="$path"
## Step Three - exit
exit 0
# test in the CLI. Once it works, it can be mounted as a CRON job using crontab -e
#Example
# every hour
#0 * * * * /your_path/random_wallpaper.sh
## Credits:
# http://stackoverflow.com/questions/701505/best-way-to-choose-a-random-file-from-a-directory-in-a-shell-script
# foxy (me)
To effect the wall-paper change at any time (triggered by the User), it is necessary to create a short cut containing the following information:
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
StartupNotify=true
Name=Pi-Wallpaper
Comment=Changes Wallpaper from Stock Pi collection
Exec=/home/pi/random_wallpaper.sh
Categories=Application;
Icon=/home/pi/Custom-Icons/LED-TV-1-icon.png