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

New auto-change desktop picture script UNIX
[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).]
    •    
  • Currently 4.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[8,424 views]  

New auto-change desktop picture script | 2 comments | Create New Account
Click here to return to the 'New auto-change desktop picture script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Documentation?
Authored by: Tastannin on Jul 10, '01 11:46:07AM

What exactly does this script do? Does it change the desktop picture upon restart? If so, they gotta be kidding - you don't restart OSX as often as you would OS9. I'd rather have a script where I could specify the time between changing the desktop picture, etc.



[ Reply to This | # ]
An AppleScript that does this nicely
Authored by: sjonke on Jul 10, '01 02:16:11PM
Proving that Unix isn't the only way to get things done, and sometimes isn't the best at all, there is a nifty AppleScript solution to having random desktop pictures (chosen randomly at login) called DesktopPictureChangerX. Drag a folder onto this nifty AppleScript and it sets that folder as the one to use for pictures. Add the script to your login prefs and at each login it runs and changes the picture for the next login by choosing randomly from the folder. The net result is that you get a different picture at every login. Get it from the authors home page: http://homepage.mac.com/kfkel/ Note that you will also need to download and install one of two scripting additions (see the web page and readme.) It works like a charm. Steve

[ Reply to This | # ]