Skip to content

Instantly share code, notes, and snippets.

@johnnyg
Created January 22, 2014 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnnyg/8551699 to your computer and use it in GitHub Desktop.
Save johnnyg/8551699 to your computer and use it in GitHub Desktop.
Randomly change desktop background
#!/bin/bash
# Changes desktop background to a pseudo-random background
# Set defaults
BG_DIR="$HOME/.backgrounds"
BG_LIST=(`cd $BG_DIR && ls *.{jp{e,}g,png,gif} 2> /dev/null`)
CURRENT_BG="`gsettings get org.gnome.desktop.background picture-uri | tail -c +8`"
NEW_BG=$CURRENT
BG_SET=0
SAME_BG=0
CHANGE_BG=1
WEIGHT_FILE="$BG_DIR/.weightings"
ABORT=0
# Functions
function change_bg () {
# Find new pseudo-random background
while [ $BG_SET -eq 0 ]; do
let "INDEX = $RANDOM % ${#BG_LIST[@]}"
NEW_BG="$BG_DIR/${BG_LIST[$INDEX]}"
if [[ "$NEW_BG" != "$CURRENT_BG" || $SAME_BG -eq 1 ]]; then
BG_SET=1
fi
done
# Change background
setBackground "$NEW_BG"
# Change speed dial
setSpeeddial "$NEW_BG"
}
function create_weights () {
# Check to see if weight file already exists
if [ -f $WEIGHT_FILE ]; then
echo "Weightings file already exists, overwrite?"
select OPTION in "yes" "no"; do
if [ "$OPTION" = "yes" ]; then
rm "$WEIGHT_FILE"
echo "Overwriting existing file"
else
ABORT=1
fi
break
done
fi
if [ "$ABORT" -eq 1 ]; then
echo "Aborting"
else
# Get weighting for each background
for BG in ${BG_LIST[@]}; do
echo -n "Weighting for $BG: "
read WEIGHTING
if [[ $WEIGHTING -ge 0 && $WEIGHTING -lt 10 ]]; then
echo "$BG:$WEIGHTING" >> $WEIGHT_FILE
fi
done
fi
}
# Process flags
while getopts ":ws" FLAG
do
case "$FLAG" in
'w') CHANGE_BG=0 ;;
's') SAME_BG=1 ;;
'*') ;;
esac
done
shift $((--OPTIND))
# Work out which function to call
if [ "$CHANGE_BG" -eq 1 ]; then
change_bg
else
create_weights
fi
#!/bin/bash
# Sets the given image as the desktop's background image
if [ $# -ne 1 ]; then
echo "Usage: $0 <image>"
else
# Log background change
logger "Changing background to $1"
# Change background
gsettings set org.gnome.desktop.background picture-uri "'file://$1'"
fi
#!/bin/bash
# Sets Opera's Speed Dial's background image to the given image
# Set default
OPERA_DIR=~/.opera-next
if [ $# -ne 1 ]; then
echo "Usage: $0 <image>"
else
# Set background
sed -i "s:^Filename=.*$:Filename=$1:" $OPERA_DIR/speeddial.ini
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment