Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to change desktop background in AppleScript in 10.7?

I'm just now upgrading from 10.6 to 10.7, and I'm having trouble with a script that I wrote that worked fine under 10.6. In part, it executes the following command:


osascript -e "tell application \"Finder\" to set destop picture to POSIX file \"<path>\""


to change the desktop background image to one of two different files based on other conditions.


In 10.7, this command completes with no error code, but it only changes the background of one of my four desktops. Is there a way to programmatically change the background image of all of my desktops simultaneously? Failing that, is there a way to a) find out how many desktops I have and b) loop through them, setting each of their backgrounds in turn?


I did search the web and found the following suggestion:


osascript -e "tell application \"System Events\" to set picture of every desktop to \"<path>\""


Unfortunately, that also changes only the current desktop's picture.


The larger script is in Perl, so I'd prefer a technology that works well with that language, but if I have to change to a different scripting language, that's not the end of the world.


Thanks much for any suggestions!


Richard

Posted on Mar 23, 2012 12:47 PM

Reply
7 replies

Mar 23, 2012 4:17 PM in response to RCobbe

Maybe you might want to try the following script as a starting point.


The script assumes that the keyboard shortcuts ^1, ^2, ^3, ^4, etc. (for switching to the corresponding desktop) are activated in the Keyboard Shortcut pane of the Keyboard System Preferences.


set N to 4 -- number of desktops

set theFile to POSIX file "/Library/Desktop Pictures/Isles.jpg" -- just an example

repeat with k from 18 to (18 + N - 1) -- 18 is the key code for "1"

tell application "System Events" to key code k using {control down}

delay 1

tell application "Finder" to set desktop picture to theFile

delay 1

end repeat

Mar 23, 2012 4:53 PM in response to RCobbe

Here's a somewhat more sophisticated version of the preceding script. However, this version requires that you first enable access for assistive devices in the Universal Access System Preference pane.



set theFile to POSIX file "/Library/Desktop Pictures/Isles.jpg" -- just an example


-- Find out how many desktops you have:

tell application "System Preferences"

reveal anchor "shortcutsTab" of pane id "com.apple.preference.keyboard"

tell application "System Events" to tell window "Keyboard" of process "System Preferences"

set N to count (UI elements of rows of outline 1 of scroll area 2 of splitter group 1 of tab group 1 whose name begins with "Switch to Desktop")

end tell


quit

end tell


-- Loop through the desktops, setting each of their backgrounds in turn:

repeat with k from 18 to (18 + N - 1) -- 18 is the key code for "1"

tell application "System Events" to key code k using {control down}


delay 1

tell application "Finder" to set desktop picture to theFile


delay 1

end repeat


Message was edited by: Pierre L.

Mar 23, 2012 7:04 PM in response to Pierre L.

Thanks very much! I'm a little disappointed that OS X doesn't provide an API to query the number of desktops directly, but given that omission, this is likely the best way to get that info.


I'm still a bit of a novice when it comes to writing AppleScript (though not to programming in general) -- I certainly wouldn't have known how to find the id of the correct pane in System Preferences, or how to scrape the screen to find the desktop count. I'd love to know a good reference for AppleScript that explains some of this stuff; I find Apple's documentation on this point to lack some of the details that I'd need explained.

Mar 23, 2012 7:15 PM in response to RCobbe

Actually, “repeat with k from 18 to (18 + N - 1)” was not a good idea, since it works only with N < 5, as I discovered after posting the script.


So here's an improved version of the previous script that should work with any number of desktops:


set theFile to POSIX file "/Library/Desktop Pictures/Isles.jpg" -- just an example


-- Find out how many desktops you have:

tell application "System Preferences"

reveal anchor "shortcutsTab" of pane id "com.apple.preference.keyboard"

tell application "System Events" to tell window "Keyboard" of process "System Preferences"

set N to count (UI elements of rows of outline 1 of scroll area 2 of splitter group 1 of tab group 1 whose name begins with "Switch to Desktop")

end tell

quit

end tell


-- Loop through the desktops, setting each of their backgrounds in turn:

tell application "System Events" to key code 18 using {control down}-- Desktop 1

tell application "Finder" to set desktop picture to theFile

repeat (N - 1) times

delay 1

tell application "System Events" to key code 124 using {control down} -- ⌘→

delay 1

tell application "Finder" to set desktop picture to theFile

end repeat

Mar 23, 2012 7:27 PM in response to RCobbe

I'd love to know a good reference for AppleScript that explains some of this stuff; I find Apple's documentation on this point to lack some of the details that I'd need explained.


If you're a novice in AppleScript, I would suggest this learning path:


1. Go to this Web page and from there to the Beginner's Tutorial.


2. After that, you might want to have a look at the AppleScript Language Guide.


3. You might also want to have a look at GUI Scripting.


Hope it can help.

Mar 27, 2012 3:28 PM in response to RCobbe

RCobbe wrote:


osascript -e "tell application \"System Events\" to set picture of every desktop to \"<path>\""


Unfortunately, that also changes only the current desktop's picture.


Imho there is only one desktop (for each connected monitor).

It sounds confusing since Mission Control shows several "desktops". But the internal name for them is still "spaces".


The larger script is in Perl, so I'd prefer a technology that works well with that language, but if I have to change to a different scripting language, that's not the end of the world.


How about a shell script? This one works with only one monitor connected:

defaults write com.apple.desktop Background '{default = {ImageFilePath = "/path/to/your/image"; }; }'; killAll Dock;



Spazek

How to change desktop background in AppleScript in 10.7?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.