Automatically change the desktop picture

Mar 18, '01 07:35:05PM

Contributed by: robg

[Warning!! Do not run the script in the comments here! It will work, but after a few uses, it will cause minor system errors. Use at your own risk! See the comments for a discussion on alternatives and next steps...]

A posting over on the MacNN forums got me interested in trying to come up with a way to automatically change the desktop picture. If I can find a way to change it from the terminal, then the next steps become somewhat obvious (and maybe easy?) -- write a shell script to pick a file at random from a folder, and then issue the command to update the desktop pic and get it to display. Finally, schedule that shell script as a 'cron' job to run however often you'd like a new desktop picture.

I made a bit of progress, but not much. Please read the rest if you're interested in trying to help me figure out how to make desktop pictures change automatically...

Here's what I posted over on MacNN:

The current desktop image is stored in com.apple.finder.plist, which lives in:
/Users/username/Library/Preferences. The problem is, it's stored inside a key that contains all the finder prefs:

DesktopViewOptions = {
ArrangeBy = grid;
BackgroundFlags = 0;
BackgroundPictureURL = "file://localhost/path/to/pic.jpg";
BackgroundType = PctB;
IconSize = 48;
};
Those were my current settings, which you can read on your machine by typing defaults read comp.apple.finder. I was able to change the desktop picture by issuing the following command (NOTE: I've broken it up to keep the window width small, but this was all entered without hiting RETURN:
defaults write com.apple.finder "DesktopViewOptions" '{
"ArrangeBy" = grid; "BackgroundFlags" = 0;
"BackgroundPictureURL" = "file://localhost/path/to/newpic.jpg";
"BackgroundType" = PctB; "IconSize" = 48; }'
The problem is that if you just set the 'BackgroundPictureURL', you overwrite all the other Finder settings - I know, because I tried!. In digging around the 'man' pages for defaults, I didn't see any way to set just the one sub-key value for the "DesktopViewOptions" key; you either write one top-level key, or you whatever you write to the sub-key becomes the only value saved there.

The above does work if you then quit and relaunch the finder (which you can do from the terminal window as well, but I can't remember how -- I tested it with force-quit). The script would have to have some way of restarting the finder to make the desktop refresh.

I've about reached my UNIX skills limit, but it seems that there are two ways to make this work. The first (easiest) is if someone knows how to write to just the "BackgroundPictureURL" value in the key, instead of wiping out the whole key. The second would be to have a shell script first run the command "defaults read com.apple.finder DesktopViewOptions > tempfile", and then (perl?) parse the tempfile to replace the BackgroundPictureURL setting, then (somehow) take the contents of tempfile and place it back into the "defaults write com.apple.finder DesktopViewOptions" command.

Can anyone help move this forward a bit more? It may not be very practical, but it's teaching me more about UNIX and Mac OS X, which is a good thing.

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20010318193505914