Skip to content

Instantly share code, notes, and snippets.

@s7dhansh
Created April 15, 2011 10:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s7dhansh/921514 to your computer and use it in GitHub Desktop.
Save s7dhansh/921514 to your computer and use it in GitHub Desktop.
Xubuntu: Random Wallpaper from a folder (recursive)
#!/bin/bash
# script: ranbg
# version 11.04
# description: randomly replace gnome background with one from a directory
# credits: Sudhanshu [http://s7dhansh.wordpress.com]
# license: GPL
# Requires imagemagick for xfce
# === User Options =============================================================
WP_PATH="$HOME/.wallpapers/"
EXTENSIONS="jpg png jpeg JPG PNG"
TMP_DIR="$HOME/.tmp"
TMP_BG_LIST="$TMP_DIR/bg_change_list"
# === End User Options =========================================================
help () {
cat << EOF
ranbg version 11.04
Set ups random desktop wallpapers
Usage: ranbg [OPTIONS]
ranbg --rm
ranbg --help
Options:
--rm deletes the current wallpaper and uses another
--help shows help options
Current wallpaper folder for resume files: $WP_PATH
Current tmp folder for resume files: $TMP_DIR
EOF
exit
}
# Basic random wallpaper generation
generateWallpaper () {
rm -f $TMP_BG_LIST
for extension in $EXTENSIONS
do
find $WP_PATH -iregex ".*.$extension" >> "$TMP_BG_LIST"
done
cnt=`wc -l "$TMP_BG_LIST" | cut -f1 -d" "`
all_bgs=`echo \`expr $RANDOM % $cnt\``
selected_bg=`head -n$all_bgs "$TMP_BG_LIST" | tail -n1`
echo "$selected_bg"
echo -e "$selected_bg">$HOME/.tmp/wallpaper
}
exportDbus () {
# Get the pid of nautilus
nautilus_pid=$(pgrep -u $LOGNAME -n nautilus)
# If nautilus isn't running, just exit silently
if [ -z "$nautilus_pid" ]; then
exit 0
fi
# Grab the DBUS_SESSION_BUS_ADDRESS variable from nautilus's environment
eval $(tr '\0' '\n' < /proc/$nautilus_pid/environ | grep '^DBUS_SESSION_BUS_ADDRESS=')
# Check that we actually found it
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
echo "Failed to find bus address" >&2
exit 1
fi
# export it so that child processes will inherit it
export DBUS_SESSION_BUS_ADDRESS
}
gnome () {
nautilus=$(gconftool-2 -g /apps/nautilus/preferences/show_desktop)
if [ "$nautilus" == "true" ]; then
# If nautilus is managing desktop
exportDbus #For cron
gconftool-2 -t string -s /desktop/gnome/background/picture_filename "$selected_bg"
else
# Else use feh
export DISPLAY=:0.0 #For cron
feh --bg-scale "$selected_bg"
fi
}
xfce () {
echo -e "# xfce backdrop list\n$selected_bg">$HOME/.config/xfce4/desktop/backdrop.list
xfdesktop -reload
rm $TMP_DIR/splash.jpg
cp "$selected_bg" $TMP_DIR/splash.jpg
convert $TMP_DIR/splash.jpg -scale X772 $TMP_DIR/splash.jpg
convert $TMP_DIR/splash.jpg -gravity south -background black -splice 0x24 $TMP_DIR/wallpaper.jpg
}
deleteWallpaper () {
wp=`cat $HOME/.tmp/wallpaper`
echo "Deleting: $wp"
rm -i "$wp"
}
# parse command line
index=0
while [ "$1" != "" ];
do
if [ "${1:0:1}" = "-" ]; then
case "$1" in
--help | -help )
help
;;
--rm )
deleteWallpaper
;;
* )
echo Unknown option $1
exit 1
;;
esac
else
let "index+=1"
case $index in
* )
echo Too many arguments
help
;;
esac
fi
shift
done
generateWallpaper
# Check sessions: gnome or xfce
gnome_pid=$(pgrep -u $LOGNAME -n gnome-session)
xfce_pid=$(pgrep -u $LOGNAME -n xfce4-session)
if [ -z "$xfce_pid" ]; then
if [ -z "$gnome_pid" ]; then
echo "Neither Gnome nor XFCE"
exit
fi
##---------------- FOR GNOME --------------
gnome
exit 2
##---------------- END GNOME----------
fi
##----------------- FOR XFCE ---------------
xfce
exit 3
##-------------- END XFCE ------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment