You are not logged in.
Hi guys,
I wanted to add a little bling to my BL by adding an "automatic wallpaper change" feature.
So I wrote this bash script that work quite nicely :
#! /bin/bash
# wallpaper-changer.sh
# v.1
#________________________________________________________________________________________________________
# This script is based on rotate-wallpaper from Philip Newborough <corenominal@corenominal.org>.
# It provides a wallpaper change among the files contained in the directory specified as arg1 ($1),
# every periode of time set as arg2 ($2).
# Two modes can be set as arg3 ($3) : linear (default: change as sorted in the directory) or random.
# Specifie r or R to get the random mode.
# Use : ./ wallpaper-changer.sh /path/of/your/directory time_in_seconds mode
# example : ./ wallpaper-changer.sh ~/home/user/wallpapers 3600 r or ./ wallpaper-changer.sh ~/home/user/wallpapers 60
# You can launch it at startup by entering the following command in your autostart :
# ## Launch wallpaper-changer
# (sleep 10s && /home/user/bin/wallpaper-changer.sh ~/home/user/wallpapers 3600 r) &
# Note1 : It does not do any control of the file's type, so make sure the specified directory
# only contains image files.
# Note2 : Can't work if file's names contain any spaces.
#_______________________________________________________________________________________________________
path="$1"
# If argument 1 is empty, exit the script with error message (if launched in a terminal).
if [ -z "$1" ]; then
echo "arg1 is empty, specify image files path"
exit 1
fi
delay="$2"
# If argument 2 is empty, use 600 seconds as default.
if [ -z "$2" ]; then
delay=600
fi
mode="$3"
# If argument 3 is empty, set it as l (for linear)
if [ -z "$3" ]; then
mode=l
fi
NUM=0
while true
do sleep $delay
# list the files in the directory :
ALIST=( `ls -w1 "$path"` )
# Count the files :
RANGE=${#ALIST[@]}
if [ $mode = "r" ] || [ $mode = "R" ]; then
# Random mode :
NEWNUM=$NUM
while [[ $NEWNUM = $NUM ]]
do let NEWNUM="$RANDOM % $RANGE"
done
NUM=$NEWNUM
else
# Linear mode :
let NUM="$(($NUM+1)) % $RANGE"
fi
# Change the wallpaper using nitrogen :
nitrogen --set-auto $path/${ALIST[$NUM]}
done
exit 0
I'm realy happy with these few lines.
But, to share with you guys, I tried to make a little bit more "bulletproof" version:
#! /bin/bash
# wallpaper-changer.sh
# v.2
# This script is based on rotate-wallpaper from Philip Newborough <corenominal@corenominal.org>.
# It provides a background image change, among the files contained in the folder specified as arg1 ($1),
# every timelap set as arg2 ($2).
# Two modes can be set as arg3 ($3) : linear (default: change as sorted in the directory) or random.
# Specify r or R to get the random mode.
# Use : ./ wallpaper-changer.sh /path/of/your/directory time_in_seconds mode
# Example : ./ wallpaper-changer.sh ~/home/user/wallpapers 3600 r or ./ wallpaper-changer.sh ~/home/user/wallpapers 60
# You can launch it at startup by entering the following command in your autostart file :
# ## Launch wallpaper-changer
# (sleep 10s && /home/"user"/bin/wallpaper-changer.sh ~/home/user/wallpapers 3600 r) &
#_______________________________________________________________________________________________________
# Change the Internal Field Separator to tab and newline only (no space) :
IFS=$'\t\n'
path="$1"
# If argument 1 is empty, exit the script with error message (if launched in a terminal).
if [ -z "$1" ]; then
echo "arg1 is empty, specify image files path"
exit 1
fi
delay="$2"
# If argument 2 is empty, use 600 seconds as default.
if [ -z "$2" ]; then
delay=600
fi
mode="$3"
# If argument 3 is empty, set it as l (for linear)
if [ -z "$3" ]; then
mode=l
fi
NUM=0
Getnewfilenumber()
{
if [ $mode = "r" ] || [ $mode = "R" ]; then
# Random mode :
NEWNUM=$NUM
while [[ $NEWNUM = $NUM ]]
do let NEWNUM="$RANDOM % $RANGE"
done
NUM=$NEWNUM
else
# Linear mode :
let NUM="$(($NUM+1)) % $RANGE"
fi
}
while true
do sleep $delay
# List the files in the directory :
ALIST=( `ls -w1 $path` )
# If ls retrieve an error, exit with an error message (if launched in a terminal):
if [ -z "$ALIST" ]; then
echo "Specified folder doesn't exist or is empty."
exit 2
fi
# Count the files :
RANGE=${#ALIST[@]}
# Get a new file number :
Getnewfilenumber
# Get the file extension (mime type)
FILE_TYPE=$(file --mime-type "$path/${ALIST[$NUM]}")
count=0
# Check if the file is an image file (.png or .jpeg). If not, get another file number :
until [ "$FILE_TYPE" = "$path/${ALIST[$NUM]}: image/png" ] || \
[ "$FILE_TYPE" = "$path/${ALIST[$NUM]}: image/jpeg" ]; do
Getnewfilenumber
FILE_TYPE=$(file --mime-type "$path/${ALIST[$NUM]}")
count=$((count+1))
# If it can't find any image file 20 times in a row, exit
# with an error message (if launched in a terminal) :
if [ "$count" = 20 ]; then
echo "Can't find any image files in the directory"
exit 3
fi
done
# Change the wallpaper using nitrogen (for other nitrogen commands, see nitrogen --help) :
nitrogen --set-auto $path/${ALIST[$NUM]}
done
exit 0
Feel free to use, share and comment
Offline
Another method, using cron: http://crunchbang.org/forums/viewtopic. … 27#p212827
And if you use feh instead of nitrogen, you can use the --randomize switch for feh to save a little work selecting a random background.
Offline
Offline
^ even simpler, and avoids parsing ls. You could also use cron to call feh and avoid having that extra sleep process (but this assumes you have cron running.)
Offline
Thanks for the comments.
So it can be done in one line with feh... I feel a little bit loosy
I'm not realy comfortable with cron, doesn't seem very user friendly : asuming I just want to modify the delay between two changes of wallpapers, I have to open a terminal, type the "crontab -e" command, edit the file with nano...
Thanks anyway
Last edited by FrenchFellow (2016-07-23 20:10:15)
Offline