Skip to content

Instantly share code, notes, and snippets.

@eramdam
Created August 22, 2014 11:52
Show Gist options
  • Save eramdam/dbe7ccef41e3784fd64a to your computer and use it in GitHub Desktop.
Save eramdam/dbe7ccef41e3784fd64a to your computer and use it in GitHub Desktop.
#!/bin/bash
## A little script that does REAL random wallpaper changing. Because the OSX's one sucks.
force=false;
while getopts ":f" opt; do
case $opt in
f)
force=true;
;;
esac
done
#Is the computer plugged in?
pluggedin=`/usr/sbin/system_profiler SPPowerDataType | grep "Connected:"`
# If the computer isn't plugged, then no random wallpaper because it drains too much battery
if [[ "$pluggedin" == *No* && $force == false ]]
then
exit
fi
PrimaryMonitor=$1
SecondaryMonitor=$2
wallpaperSource=~/Dropbox/Wallpapers/*/*
wallpaperDestination=~/Pictures/Wallpapers
TIMESTAMP=$(date +%H:%M:%S)
changeWallpaper() {
WALLPATH=`readlink $1`
PROTECTEDMONITOR="$PrimaryMonitor"
MONITORCHANGED="$SecondaryMonitor"
osascript << END
tell application "Finder"
set wall to "$WALLPATH"
tell application "System Events"
set theDesktops to a reference to every desktop
repeat with x from 1 to (count theDesktops)
if name of item x of the theDesktops is "$MONITORCHANGED" then
set picture of item x of the theDesktops to wall
end if
end repeat
end tell
end tell
END
}
primaryWallpaper() {
osascript << END
tell application "Finder"
tell application "System Events"
set theDesktops to a reference to every desktop
repeat with x from 1 to (count theDesktops)
if name of item x of the theDesktops is "$1" then
return picture of item x of the theDesktops
end if
end repeat
end tell
end tell
END
}
priWall=$(primaryWallpaper "$PrimaryMonitor")
files=(${wallpaperDestination}/*)
wallpaperCount=`ls $wallpaperDestination/*.{jpg,png} | wc -l`
# ## Taking a random file from the folder
randomWallpaper="${files[RANDOM % ${#files[@]}]}"
# ## Setting it as the current wallpaper
changeWallpaper $randomWallpaper
echo "$TIMESTAMP wallpaper changed on $PrimaryMonitor to $randomWallpaper"
if [ $wallpaperCount -gt 1 ]
then
rm $randomWallpaper
echo "$TIMESTAMP wallpaper file ($randomWallpaper) removed. Now $wallpaperCount wallpaper(s) remaining"
else
rm $wallpaperDestination/*.jpg
rm $wallpaperDestination/*.png
ln -s $wallpaperSource $wallpaperDestination
echo "$TIMESTAMP Wallpaper collection rebuilt"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment