Randomize Gnome wallpaper

Lately, different projects seem to emerge to manage random wallpapers. I have been using this script, which I’m calling from cron every X time to change my Gnome desktop background.

#! /bin/bash
if [ "$1" = "disable" ]
then
touch ~/Pictures/.no_set_background
fi
if [ "$1" = "enable" ]
then
if [ -f ~/Pictures/.no_set_background ]
then
rm ~/Pictures/.no_set_background
fi
fi
if [ -f ~/Pictures/.no_set_background ]
then
exit 0
fi
# File locations
FilePath=~/Wallpapers/
TempLog=/tmp/filelist.log
# Create a temporary logfile of all matches
cd $FilePath
find -L $FilePath -iregex ".*.jpg" > $TempLog
# Choose a random line number (any number from 1 to the length of the file)
LowerBound=1
RandomMax=32767
UpperBound=$(cat $TempLog | wc -l)
RandomLine=$(( $LowerBound + ($UpperBound * $RANDOM) / ($RandomMax + 1) ))
# Use sed to grab the random line
File=$(sed -n "$RandomLine{p;q;}" "$TempLog")
# use the random line
gconftool -t string --set /desktop/gnome/background/picture_filename "$File"

And, yes, Philip, I’m proud of using Ubuntu. I’m also a happy FreeBSD user, btw.

Categories: All 0 comments