Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Perl solution' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Perl solution
Authored by: robh on Mar 19, '01 08:52:06AM

This Perl script seems to work. Don't blame me if it breaks anything :-). Note that I don't know if HUPing the Desktop process is the right way to activate the change but it seems to work.

#!/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

use strict;

my $pic_dir = "/Users/rob/Documents/Images";  # 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)];

# insert the URL for the image
$prefs =~ s|(BackgroundPictureURL = )"?([^;]+)"?;|$1"file://localhost$pic_dir/$new_pic";|;
# switch on background image
$prefs =~ s/(BackgroundType = )"?([^;]+)"?;/${1}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 Desktop`;
$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";
}



[ Reply to This | # ]
Perl solution
Authored by: robh on Mar 20, '01 10:20:54AM

hold off using my script. After using it a few times it stops working - the kill signal to restart the Desktop process will eventually fail with the desktop error 'The system has detected a minor problem and repaired it. Some of your preferences may have changed'.

Anyone know of a way to talk to the Desktop (aka Finder) ?. I did some research into Applescript but couldn't locate anything that'd tell the process to refresh the root window. It's possible to communicate with Desktop with this ...
/usr/bin/osascript 'tell application "Desktop" to XXXX'
but I can't work out what instruction (XXXX) to give it.

I also prefer the other poster's suggestion of changing the picture file instead of the .finder prefs. It's easy to change my Perl script to do that, but there's still a need to find a way to tell Desktop to do the refresh.



[ Reply to This | # ]