Desktop Pictures


Is there a way to make OSX automatically to select a new desktop picture every time you login/reboot?


Official Volunteer
Originally posted by TheFiler
Is there a way to make OSX automatically to select a new desktop picture every time you login/reboot?
Well, your desktop image is stored as a Finder property, so someone with a bit
of Applescript/shell-scripting kung fu could probably rig up a script that
would randomly select an image from a directory and set the picture
woth a "defaults write"...


I know perl...if desktop pictures are done with the "defaults write apple..." way, and can be done in the termminal, I could definitly write a script, it'd be ez...

If someone can get me the exact command ("Defaults Write...") I'll hook it up:)



As far as how to change the desktop picture from the command line, you can do it like this (all one line):

defaults write DesktopViewOptions -dict-add BackgroundPictureURL "file://localhost/Users/username/Pictures/picture.jpg"

obviously replacing username with the right one and 'Pictures/picture.jpg' with the right path to the picture. Since it's an URL, you may also be able to point it to some regularly-updating picture on the web.

However, if this is done on login, it has to be done before the Finder starts, which may be tricky. The other option would be to change it on logout, so the next login picks up the change...


It doesn't actually have to be done at login - it could be changed couldn't it after log in for the next time that you login. I would suggest using the .login file which is run every time there is a login onto the computer. It us usually found in the user's home directory. The period at the beginning of the name means that it will be hidden by default.

I haven't tried this yet, but I should think that it is worth a go. The only issue that I can think of is making sure that we know when the desktop picture is loaded by the OS. I presume that it only occurs at login and when you force it by changing the picture. The .login is quite a neat file - I used to use it a lot with Xwindows to load up my applications and place them exactly where I wanted. The benefit was that it could detect which machine I was on and run a different initialise script for each one.

Actually, as blb says this could be run on the logout. I actually would prefer that for data integrity - the desktop picture would then show the picture that is stated in the settings. The .logout file is the one to look for to script up logouts.

It is worth remembering that this will run every time that you log in/out of the machine. Opening a terminal counts as a login (again I haven't checked this, but why should it be different to other UNIXs). There are ways around this by checking the type of terminal being opened, etc. within the login/logout script.

Red Phoenix

I only just came across this thread. I have written the following into a file, to randomly select a desktop background from my folder full of desktop backgrounds:



$background_path="/Volumes/Icarus/Desktop Backgrounds";

$raw=`find "$background_path" \\( \\( -name "*.jpg" -or -name "*.jpeg" \\) -or -name "*.tiff" \\) -print`;
$the_number=1+ int rand scalar @the_files;
`defaults write DesktopViewOptions -dict-add BackgroundPictureURL "file://localhost$the_file"`;


I'm only just learning Perl, so I am not sure if array lengths start at 1 or 0. For me, this will only come up 1/250th percent of the time, so I like those odds. Anyway, one should need only to change the $background_path to get this to work elsewhere.

Now, personally I run a cron job that changes my desktop background every couple of hours. Right now, I'd have to log in and out every couple of hours to appreciate it, though. Has anyone found a way to get the Finder to refresh itself? I know applications can do it, but can Perl scripts?

Red Phoenix

One other thing I forgot to mention was that, because the file is mentioned as a URL in the preferences, I had to correct the space to be "%20". Any other special character will make it balk. I am sure that Perl ought to be able to fix special characters, it being used for CGI's so often, but I have no idea how.


There is a function in Perl that will convert between the actual text and the url format required. The function is 'urlencode'. It is a very handy function.


Red Phoenix

Thanks. This sounds exactly like what I'm looking for. A quick look at the man for perlfunc shows that it's not there, so I presume that it's in a Perl mod. I guess it's time to go through the mans for perlboot, perltoot, and perltootc to figure out what mods are out there... Thanks again.

-Bruce Adcock


Angry Member

That's the URLEncode subroutine (or an implementation of it).

You should be able to just toss this routine into the file that changes the background and have everything be golden. Alternately, you can start building a library of functions for your own use, place it somewhere where your scripts can access it, and require it (the perl equiv. of import in java and include in C). Making your code modular in this way can go a long way to helping you better learn the language, because you will tend to discover subtle nuances in your older code and improve upon it, as well as avoid these pitfalls in subsequent code (at least, you are more(less?) likely to).

A fair number of perl CGI scripts include a library of CGI functions (the name of this library eludes me right now) but it implements things like seperating the hash into name-value pairs etc. etc. I'm fairly certain that this library includes a urlencode subroutine.

Then again, I hate perl :)


Im not completely sure but i remember that when i was learning html adn building little web pages that just worked on my computer but isnt the command for accessing a file on ur computer file:/// (3 slashes not 2)??