Skip to content

Instantly share code, notes, and snippets.

@dtrailin
Created January 17, 2013 22:09
Show Gist options
  • Select an option

  • Save dtrailin/4560275 to your computer and use it in GitHub Desktop.

Select an option

Save dtrailin/4560275 to your computer and use it in GitHub Desktop.
Change destop background in gnome 3
BACKGROUND_PATH="/home/denis/Pictures/AT_wallpapers/"
BACKGROUND_STYLE="scaled" ## 'zoom', 'tiled', 'scaled', 'stretched', 'centered'
CYCLE_TIME="300"
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 <SOURCE_DIRECTORY> -t <CYCLE_TIME> -k <KEYWORD>"
echo "Gnome Background Rotator"
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
gsettings set org.gnome.desktop.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}"
gsettings set org.gnome.desktop.background picture-uri file:///"${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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment