#!/bin/bash # Wunderbackground script for OS X. # Copyright 2005 Perette Barella. # Public domain. # There is no warranty for this software. Use at your own risk. # # This script converts cached files from the Wunderground screensaver # into jpeg files by stripping of the headers Wunderground puts there. # It then places them in the Library/Cache directory, where you can # point the file selector for desktop backgrounds and get random # backgrounds new each day... or whenever your screensaver runs. # # Dependencies: ImageMagick # # To install this: # Create a folder called "bin" in your home directory, and put # this file in it named "wunderbackground-osx" # Open a terminal window (Applications->Utilities->Terminal), and type: # chmod u+x bin/wunderbackground-osx # crontab -e # Go # 18 5 * * * ~/bin/wunderbackground-osx ZZ # bin/wunderbackground-osx # Watch your capitalization. Add after each line, and # press the escape/esc key for , don't type it in. # You are done! The background images will be updated at 5:18 every morning. # Use ImageMagick from Fink PATH="/sw/bin:$PATH" # The desktop random displayer can stretch an image, but distorts it # if it isn't in proportion to the screen. It can fill the screen, # but then only displays a portion of an image. There is no "Make it # as big as possible without distorting it" option. Do it ourselves. IMAGEWIDTH=1024 IMAGEHEIGHT=768 BADSCREENSAVER="$HOME/Library/Preferences/Screentime prefs/cache" SCREENSAVER="$HOME/Library/Caches/Screentime" IMAGECACHE="$HOME/Library/Caches/Wunderground/Images" KEEPDAYS=5 ###################################################################### # Function: resize_jpeg # Purpose: Resize a jpeg (making a thumbnail) and write out the new file. # Arguments: $1 - the file to resize. # Returns: 0 on success, non-0 on error # Author: Perette Barella #--------------------------------------------------------------------- function resize_jpeg { typeset file="$1" SIZE x y dirname adjwidth adjheight adjust [ $IMAGEWIDTH -eq 0 -o $IMAGEHEIGHT -eq 0 ] && return 0 dirname=$(dirname "$file") typeset resized="$dirname/$$.temp.jpeg" set $(identify -format "%w %h" "$file") || exit 1 x="$1" y="$2" # calculate the adjustment for width and height as a per thousand. let adjwidth="(IMAGEWIDTH * 1000) / $x" let adjheight="(IMAGEHEIGHT * 1000) / $y" typeset adjust=$adjwidth [ $adjheight -lt $adjwidth ] && let adjust=$adjheight let newwidth="(x * adjust) / 1000" let newheight="(y * adjust) / 1000" if convert -quality 95 -geometry ${newwidth}x${newheight} "$file" "$resized" then mv "$resized" "$file" else rm "$resized" fi return $? } ##### End of function resize_jpeg ##### function clean_older { typeset minimum="$1" directory="$2" typeset days=1 file [ $(find "$2" -type f -print | wc -l) -le $minimum ] && return 1 # See how far into past we need to go to keep enough files to # meet minimum. while [ $(find "$2" -type f -mtime -$days -print | wc -l) -lt $minimum ] do let days=days+1 done find "$2" -type f -mtime +$days -print | while read file do rm "$file" done return 0 } # If the damned cache file is in the preferences directory, move it # into the cache directory where it should be. A change request # has been submitted to the ScreenTime folks. if [ ! -d "$SCREENSAVER" ] then mkdir "$SCREENSAVER" || exit 1 # Move the cached files if ! mv "$BADSCREENSAVER/"* "$SCREENSAVER" then rmdir "$SCREENSAVER" exit 2 fi # Remove the old cache directory, and link to the corrected one. rm -rf "$BADSCREENSAVER" ln -s "$SCREENSAVER" "$BADSCREENSAVER" fi # Now create the new cache directory, clear it out, and convert files into it. mkdir -p "$IMAGECACHE" || exit 1 # For some reason, the screensaver doesn't clear it's cache directory # (Maybe it's confused by moving the files, or maybe it's just stupid.) # Clean up older crap in the screensaver cache when it's old. clean_older 40 "$SCREENSAVER" for file in "$SCREENSAVER"/*/*_ok do # Copy the file, stripping off the header destfile="$IMAGECACHE/$(basename "$file").jpeg" [ -f "$destfile" ] && continue tail +4 "$file" > "$destfile" # Make sure it's a jpeg file jpeg=false [ $(file "$destfile" | grep -c JPEG) -gt 0 ] && jpeg=true if $jpeg then resize_jpeg "$destfile" else rm "$destfile" fi done clean_older 40 "$IMAGECACHE" exit 0