3

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?

6 Answers 6

5

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.

2
  • 24:113: execution error: Finder got an error: Can’t make "/Library/Desktop Pictures/Beach.jpg" into type file. (-1700)
    – iconoclast
    May 17, 2012 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, 2012 at 21:28
3

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.

2

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.

0

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.

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

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...

1
  • 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, 2009 at 6:53
0

In my case there was "Change Picture" selected in "Desktop & Screen Saver" menu. It's still possible to uncheck this programatically:

/usr/bin/osascript -e 'tell application "System Events"
  tell current desktop
    set picture rotation to 0
  end tell
end tell
'

Now you can use this:

/usr/bin/osascript -e 'tell application "System Events" to set picture of current desktop to "/Users/user/Downloads/pictures/1.jpg"'

If you want to restore it, change 0 to 1.

Also interval and order can be adjusted with:

    set change interval to 60.0
    set random order to true

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .