Gnome Background Rotator

Version: v0.5

Summary: This is an easy way to make your desktop rotate images. All you need to do is specify a background location, and it will find all the *.jpg, *.png, and *.svg files in that folder and display them in a random order.

Requirements:

  • Gnome
  • find
  • sed
  • wc
  • whoami

Code:

#!/bin/bash
 
#User Settable Variables (Defaults)
BACKGROUND_PATH="/usr/share/.backgrounds/"
BACKGROUND_STYLE="zoom" ## 'zoom', 'tiled', 'scaled', 'stretched', 'centered'
CYCLE_TIME="7"
TEMP_LOG="/tmp/rotate${RANDOM}.log"
LOG="/tmp/.`whoami`-gnome-background-rotator"

##Write Date & Time to Log File
echo "-------------------------------------" > "${LOG}"
echo "`date +%B %d, %Y`" >> "${LOG}"
echo "-------------------------------------" >> "${LOG}"
 
USAGE="Usage: ${0} -p  -t  -k "
 
echo "Gnome Background Rotator"
echo "Scripted by Blake Johnson"
echo "http://www.simplescripts.net/"
echo
 
while getopts ':p:k:t:h' OPTION; do
	case ${OPTION} in
		p) BGPATH="${OPTARG}";;
		k) KEYWORD="${OPTARG}";;
		t) CTIME="${OPTARG}";;
		h) echo "${USAGE}" >&2
			exit 2;;
		?) echo "Unknown option "-${OPTARG}"." >&2
			echo ${USAGE};;
		:) echo "Option "-${OPTARG}" needs an argument." >&2
  			echo ${USAGE}
  			exit 2;;
  		*) echo ${USAGE}
  			exit 2;;
  	esac
done

if [ "$BGPATH" ] #Check to see if another background source was given.
then
	if [ -d "$BGPATH" ]
	then
		BACKGROUND_PATH="${BGPATH}"
	else
		echo "Unable to continue.  Source specified is not a (readable) directory."
		exit
	fi
fi

if [ "$CTIME" ] #Check to see if another cycle time was given.
then
	if [ "$CTIME" -gt 0 ]
	then
		CYCLE_TIME="${CTIME}"
	else
		echo "Unable to continue.  Time given is not a valid number."
		exit
	fi
fi

gconftool-2 -s --type=string /desktop/gnome/background/picture_options ${BACKGROUND_STYLE}
 
#Search for images and create a temporary logfile of all matches
if [ "$KEYWORD" ]
then
	find ${BACKGROUND_PATH} -iname "*${KEYWORD}*.jpg" -or -iname "*${KEYWORD}*.png" -or -iname "*${KEYWORD}*.svg" > ${TEMP_LOG}
else
	find ${BACKGROUND_PATH} -iname "*.jpg" -or -iname "*.png" -or -iname "*.svg" > ${TEMP_LOG}
fi

LowerBound=1
RandomMax=32767
UpperBound=$(cat ${TEMP_LOG} | wc -l)

if [ $UpperBound -gt 0 ] #Check to make sure 1 or more results have been returned.
then
	echo "Starting to rotate through ${UpperBound} images(s)."
else
	echo "No images found.  Please change directories, or try another keyword."
	exit
fi

while [ "true" ]
	do
		# Choose a random line number (any number from 1 to the length of the file)
		RandomLine=$(( ${LowerBound} + (${UpperBound} * ${RANDOM}) / (${RandomMax} + 1) ))
 
		# Use sed to grab the random line
		IMAGE=$(sed -n "$RandomLine{p;q;}" "${TEMP_LOG}")
 
		if [ -r $IMAGE ]
		then	
        		echo "Changing background to: ${IMAGE}"
        		gconftool-2 -s --type=string /desktop/gnome/background/picture_filename "${IMAGE}"
        		sleep ${CYCLE_TIME}
        	else
        		echo "Unable to change the background to: ${IMAGE}"
        		echo "The background either does not exist anymore, or the permissions to it are denied."
        	fi
	done
done

Changes:
v0.5: Ability to adjust cycle-time and background directory at command line, and also specify a background keyword. More checks added to make sure everything works as expected.
v0.4: Minor fixes.
v0.3: Backgrounds are now randomized.
v0.2: Minor fixes.
v0.1: Initial release.

Credits: Special thanks to Kevin Warns for implementing the randomization feature.

2 thoughts on “Gnome Background Rotator”

Leave a Reply to Gustavo Cancel reply

Your email address will not be published.