[Editor's note: Submitted by Tony Williams on July 5th. I have not personally tested this script yet, so use at your own risk.]
Hi,
I did some hacking and testing of robh's Perl script to auto change the desktop picture. Step one was to fix the way it killed the Desktop rather than the Finder (much safer). The other was to fix the handling of the file URL so that it worked with all Mac file names, including ones with spaces or other characters.
Read the rest of this article if you'd like the new and improved script.
#!/usr/bin/perl
# This is freeware, do whatever you want with it apart from passing it
# off as your own work.
# Use it at your own risk. It has been tested on Mac OSX Public Beta.
# Rob Hartill, 19/March/2001
# changes to fix file handling and update for OS X release
# by Tony Williams 5/July/2001
use strict;
use URI::file;
my $pic_dir = "/Volumes/Big/created_desks"; # change for local dir
my $prefs = `defaults read com.apple.finder`; # read existing finder prefs
# read all the jpeg images in the local dir
my $dirhandle;
opendir($dirhandle, $pic_dir) || die "Failed to open $pic_dir, $!";
my @jpegs = grep(/jpe?g$/i, readdir($dirhandle));
closedir($dirhandle);
# pick a random jpeg image
my $new_pic = $jpegs[rand(@jpegs)];
my $uri = URI::file->new($new_pic);
my $pic_path = "file://localhost$pic_dir/$uri";
# insert the URL for the image
$prefs =~ s|(BackgroundPictureURL = )"?([^;]+)"?;|$1"$pic_path";|;
# switch on background image
$prefs =~ s/(BackgroundType = )"?([^;]+)"?;/$PctB;/;
# remove old finder prefs and replace with new prefs
print STDERR `defaults delete com.apple.finder`;
print STDERR `defaults write com.apple.finder '$prefs'`;
# find the process id of the 'Desktop' process
my $pid = `ps x | grep Finder`;
$pid =~ s/^ ?(d+) .*/$1/;
# if we have a process id for the Desktop, send it a restart signal
if ($pid >0) {
kill(1, $pid);
} else {
die "Failed to find pid of Desktop processn";
}
[Editor's note: To make this into a functional OS X program, open a terminal, launch an editor such as Emacs, vi, or Pico, and copy and paste the above text. Name and save the file (say "picswitch") and then quit the editor. Make the file executable ("chmod 755 picswitch") and you should then be able to run the script ("./picswitch" in the current directory).]
Mac OS X Hints
http://hints.macworld.com/article.php?story=20010709115108957