Last active
June 19, 2016 10:04
-
-
Save kisaragi-hiu/70cd9662cd7bd345401840537ffdcf70 to your computer and use it in GitHub Desktop.
Bash script to loop random wallpaper for current desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## Untitled Random Wallpaper Slideshow Script. Name is subject to change. | |
## Edit 2016/06/17 00:23 UTC+8 | |
#### Variables #### | |
# Initialize variables with default values | |
OPTIND=1 | |
rwp_sleep_time_def=1m | |
rwp_cfg_file_set=0 | |
rwp_handler=auto | |
rwp_handlers_list="Available wallpaper handlers: mate, gnome, xfce, lxde, pcmanfm, cinnamon, feh" | |
# Get XDG_PICTURES_DIR from .config/user-dirs.dirs. Ex:"$HOME/Pictures" | |
export $(cat $HOME/.config/user-dirs.dirs | grep PICTURES) | |
# Cut from " to /. Ex:"$HOME/Picures" -> Pictures" | |
XDG_PICTURES_DIR=$(echo ${XDG_PICTURES_DIR#\"*/}) | |
# Cut off the last ". Ex: Pictures" -> Pictures | |
XDG_PICTURES_DIR=$(echo ${XDG_PICTURES_DIR%\"}) | |
# Ex. Pictures -> /home/user/Pictures/Wallpapers | |
rwp_dir=$HOME/$XDG_PICTURES_DIR/Wallpapers | |
# Use .rwp.cfg if it exists | |
if [[ -f $HOME/.rwp.cfg ]]; then | |
rwp_cfg_file_set=1 | |
rwp_cfg=$HOME/.rwp.cfg | |
else | |
touch $HOME/.rwp.cfg | |
rwp_cfg_file_set=1 | |
fi | |
# Handle Options | |
while getopts "h?t:d:c:w:l" rwp_opt; do | |
case "$rwp_opt" in | |
h|\?) | |
echo " | |
Usage: randomwallpaper [OPTIONS] | |
Options: | |
-t <time> Set global wait time (default:1m) | |
-d <DIR> Set wallpaper directory (default:$HOME/$XDG_PICTURES_DIR/Wallpapers) | |
-c <config file> Use config file to set wait time for individual images | |
-w <wallpaper handler> Set wallpaper handler manually (default:auto) | |
-l List all wallpaper handlers | |
-h or -? Show help (this message) | |
" | |
exit 0 | |
;; | |
t) | |
rwp_sleep_time_def=$OPTARG | |
;; | |
d) | |
rwp_dir=$OPTARG | |
;; | |
c) | |
rwp_cfg=$OPTARG | |
rwp_cfg_file_set=1 | |
;; | |
w) | |
case $OPTARG in | |
MATE|mate) | |
rwp_handler=mate | |
;; | |
GNOME|gnome) | |
rwp_handler=gnome | |
;; | |
XFCE|xfce) | |
rwp_handler=xfce | |
;; | |
LXDE|lxde) | |
rwp_handler=pcmanfm | |
rwp_handler_display=LXDE | |
;; | |
pcmanfm) | |
rwp_handler=pcmanfm | |
;; | |
Cinnamon|cinnamon) | |
rwp_handler=cinnamon | |
;; | |
feh) | |
rwp_handler=feh | |
;; | |
*) | |
echo $rwp_handlers_list | |
exit 1 | |
esac | |
;; | |
l) | |
echo $rwp_handlers_list | |
exit 0 | |
esac | |
done | |
rwp_files=($rwp_dir/*) | |
# Report result | |
echo "rwp_sleep_time_def is $rwp_sleep_time_def" | |
echo "rwp_dir is $rwp_dir" | |
echo "rwp_cfg is $rwp_cfg" | |
#~~~ Variables ~~~# | |
#### Functions #### | |
# Main Loop | |
# rwp_loop <WP handler> | |
rwp_loop () { | |
while :; do | |
# Rescan file list | |
rwp_files=($rwp_dir/*) | |
# Randomly select a file | |
rwp_wallpaper=$(printf "%s\n" "${rwp_files[RANDOM % ${#rwp_files[@]}]}") | |
# Read rwp_sleep_time only when config file is set | |
if [[ $rwp_cfg_file_set == 1 ]]; then | |
# Read cfg file | |
while read -r line; do | |
if echo $rwp_wallpaper | grep ${line%\ *}; then # if rwp_wallpaper is in the cfg | |
rwp_sleep_time=${line#*\ } # set rwp_sleep_time to that defined in the cfg | |
rwp_cfg_used=1 | |
echo rwp_sleep_time for this image is $rwp_sleep_time | |
break | |
else | |
rwp_cfg_used=0 | |
fi | |
done < $rwp_cfg | |
fi | |
# Use default rwp_sleep_time | |
if [[ $rwp_cfg_used == 0 || $rwp_cfg_file_set == 0 ]]; then | |
rwp_sleep_time=$rwp_sleep_time_def | |
echo rwp_sleep_time for this image is not specified in config | |
fi | |
# using variable as input needs "" to handle spaces correctly | |
rwp_set_$1 "$rwp_wallpaper" | |
sleep $rwp_sleep_time | |
done | |
} | |
rwp_detect () { | |
# Detect DE in use and start loops | |
# MATE | |
if [[ | |
$XDG_CURRENT_DESKTOP == "MATE" || | |
$XDG_CURRENT_DESKTOP == "mate" ]]; then | |
rwp_handler=mate | |
# Budgie | |
elif [[ | |
$XDG_SESSION_DESKTOP == "budgie-desktop" || | |
$DESKTOP_SESSION == "budgie-desktop" || | |
$GDMSESSION == "budgie-desktop" ]]; then | |
rwp_handler=gnome | |
rwp_handler_display=Budgie | |
# GNOME & Cinnamon | |
elif [[ | |
$XDG_CURRENT_DESKTOP == "GNOME" || | |
$XDG_CURRENT_DESKTOP == "gnome" ]]; then | |
if [[ $GDMSESSION == "cinnamon" ]]; then | |
rwp_handler=cinnamon | |
else | |
rwp_handler=gnome | |
fi | |
# Cinnamon | |
elif [[ $XDG_CURRENT_DESKTOP == "X-Cinnamon" ]]; then | |
rwp_handler=cinnamon | |
# Unity | |
elif [[ $XDG_CURRENT_DESKTOP == "Unity" ]]; then | |
if [[ -z ${MIR_SERVER_NAME+x} ]]; then # Check if Mir is in use | |
rwp_handler=gnome # It's not | |
rwp_handler_display=Unity | |
else | |
rwp_exit "Unity with Mir" # It is | |
fi | |
# XFCE | |
elif [[ $XDG_CURRENT_DESKTOP == "XFCE" ]]; then | |
rwp_handler=xfce | |
# LXDE | |
elif [[ $XDG_CURRENT_DESKTOP == "LXDE" ]]; then | |
rwp_handler=lxde | |
rwp_handler_display=LXDE | |
# KDE | |
elif [[ $GDMSESSION == "plasma_desktop" ]]; then | |
rwp_exit "KDE Plasma" | |
# feh ?? | |
# elif killall -q -0 feh; then | |
# rwp_handler=feh | |
# Other | |
else | |
rwp_exit "Desktop in use" | |
fi | |
} | |
rwp_set_mate () { | |
gsettings set org.mate.background picture-filename "$1" | |
} | |
rwp_set_gnome () { | |
gsettings set org.gnome.desktop.background picture-uri "file://$1" | |
} | |
rwp_set_cinnamon () { | |
gsettings set org.cinnamon.desktop.background picture-uri "file://$1" | |
} | |
rwp_set_feh () { | |
feh --bg-scale "$1" | |
} | |
rwp_set_pcmanfm () { | |
pcmanfm --set-wallpaper "$1" | |
} | |
rwp_set_xfce () { | |
rwp_set_xfce_wspc=$(($(xfconf-query -c xfwm4 -p /general/workspace_count)-1)) | |
for ((i=rwp_set_xfce_wspc; i>=0; i--)); do | |
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace$i/last-image -s "$1" | |
done | |
} | |
rwp_exit () { | |
echo "$1 is not supported" | |
exit 1 | |
} | |
#~~~ Functions ~~~# | |
#### Main Logic #### | |
if [[ $rwp_handler == auto ]]; then | |
# Detect Desktop | |
rwp_detect # will set rwp_handler according to environment variables | |
if [[ -z ${rwp_handler_display+x} ]]; then # Check for rwp_handler_display | |
echo "Looping random wallpaper for: $rwp_handler" # it does not exist | |
else | |
echo "Looping random wallpaper for: $rwp_handler_display" # it exists | |
fi | |
rwp_loop $rwp_handler | |
else # when rwp_handler is set | |
echo "Looping random wallpaper for: $rwp_handler" | |
rwp_loop $rwp_handler | |
fi | |
#~~~ Main Logic ~~~# | |
# Main Logic is not a function because we'll never call it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment