Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a shell script that gets run by a cron job that updates a jpeg. The desktop background on my external monitor is set to this same file. However, Mac OS X does not automatically refresh the desktop background when the file is updated.

How do I force the desktop background to be updated from the script?

share|improve this question

6 Answers 6

up vote 5 down vote accepted

A proper way (without using something else to call your script) would be to add the following line to your shell script:

osascript -e 'tell Application "Finder"' -e 'set the desktop picture to {"NAME OF STARTUP DRIVE:PATH:TO:PICTURE.jpg"} as alias' -e 'end tell'

Note: The path uses colons instead of a / and you must name the hard drive.

A really odd alternative more for giggles, is a massive kludge of a workaround is to add the following line to your shell script run by cron:

killall Dock

This will kill the Dock, which when it restarts (automatically) will refresh your desktop background. Downsides? Your open application dock icons will re-order (if the app icons aren't already saved a position in your dock) and any minimized windows will unminimize. So if you don't use those features, it's fine.

share|improve this answer
    
24:113: execution error: Finder got an error: Can’t make "/Library/Desktop Pictures/Beach.jpg" into type file. (-1700) –  iconoclast May 17 '12 at 21:06
    
@Brandon: Not sure when this broke; but if you remove "POSIX path of" it works correctly. Updated the answer accordingly. –  Chealion May 18 '12 at 21:28

Here's the final applescript I'm using:

tell application "System Events"
    set myfolder to folder "foo" of the pictures folder
    tell desktop 2 to set picture to file "empty.jpeg" of myfolder as alias
    tell desktop 2 to set picture to file "bar.jpeg" of myfolder as alias
end tell

This is passed to osascript from the shell (actually ruby) script that updates the jpeg.

The previously discussed issue of the background not updating if the new path is the same as the current path (which was still a problem with Chealion's answer) is hacked-around by temporarily switching to "empty.jpeg". This is annoying as the transition is jerky but it beats having to restart the Finder/Dock.

I'm using the System Events application because the Apple examples do too.

share|improve this answer

My previous answer to this sucked, so here's a better one.

I use a shell script and an Automator workflow to do this. The shell script downloads the image I want, saves it somewhere, and echos the full path to the image (that's the important part). The Automator workflow then looks something like this:

[run shell script ~/bin/getAPOD.sh] -> [Set the Desktop Picture]

That "Set the Desktop Picture" action, as far as I can tell, came with Automator.

I then have a crontab entry that looks like this:

* 4 * * * /usr/bin/open /Users/myuser/bin/APODset.app

If you'd like I can post the script, the Automator workflow, whatever.

share|improve this answer

I used AppleScript to set the desktop background to the file named on the commandline (or maybe stdin), and set a cron job to run that after downloading APOD. I don't have the script here, but it didn't take too long, and I'd never done that before.

[EDIT] Maybe it was Automator? Sorry, it's been a while.

share|improve this answer

It's quite simply to do this without using an external script....

First, keep your cron job....

Second, Create a folder that your cron job will place a single photo/jpg in. This folder must contain only a single photo/jpg.. (Multiple files will give you a random effect, see below).

Third, Open System Preferences -> Desktop & Screen Saver -> Desktop tab

Fourth, either drag or use the + to add the new folder to your list.

Fifth, highlight the folder in the folder list

Sixth, Turn on Change Picture, and choose a time frame.

You could also run a Applescript to reset the picture, but that's the most obvious and simplist way I can see to do that...

share|improve this answer
    
This is suboptimal (I have to set the "change picture" period considerably lower than the cronjob period to get timely updates when the two are out of phase). That said, it looks like it's the best solution. –  p00ya Sep 4 '09 at 6:53

I know it doesn't answer your question fully, but this might be interesting for you to read. The way someone did it was using an AppleScript that restarted Finder; when Finder restarts it automatically refreshes the background image; there doesn't seem to be any other way.

share|improve this answer
    
that's undesirable, having all my Finder windows killed is quite intrusive. Informative link though. –  p00ya Sep 3 '09 at 1:52
    
FWIW, Dock.app controls the background picture now and not the Finder and has since 10.3 IIRC. –  Chealion Sep 3 '09 at 5:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.