AppleScript: Change the Desktop Picture (Wallpaper) of Two (or More) Screens

November 10, 2009 19:46 by scibuff

tell application "Finder"

set path_to_file1 to POSIX file "/Users/myusername/Desktop/file1.jpg"
set path_to_file2 to POSIX file "/Users/myusername/Desktop/file2.jpg"

tell application "System Events"
set theDesktops to a reference to every desktop
set picture of item 2 of the theDesktops to file path_to_file_2
end tell

set desktop picture to file path_to_file_1
end tell

I have run into some difficulties with changing the system Desktop Picture (wallpaper), in my most recent application that downloads the Astronomy and Amateur Astronomy Picture of the Day and sets them as the Desktop Pictures on my two screens. It was fairly simple to have the main screen Desktop Picture changed via this simple code:

tell application "Finder"
set desktop picture to file path_to_file
end tell

Unfortunately, the set desktop picture works only with the main screen and so I had to come up with a solution of how to achieve the same for the second monitor. I looked through OS X boards and forums, Googled all possible variations of keywords of ‘AppleScript’, ‘desktop picture’, ‘two’, ‘second’, ‘monitor’, ‘screen’, etc, but nothing seemed to work. I couldn’t get the Desktop Picture of the second monitor to change. Either the change required a restart (or at least the Finder application), or my attempts failed altogether. Even worse, authors of the code I found were talking about exactly the same issues I had. Among all those so-much-unhelpful lines of code, I found these, that seem to show quite a bit of promise:

tell application "System Events"
set theDesktops to a reference to every desktop
set picture of item 1 of theDesktops to path_to_file_1
set picture of item 2 of theDesktops to path_to_file_2
end tell

The above code did was it was meant to do, i.e set the Desktop Picture of the two screens, but a restart was needed (I usually only killed and restarted Finder).

After some time I realized that the code I had at the beginning (for the main screen) did not have this problem … or rather that it (somehow) refreshed the wallpaper data immediately. Finally I thought that maybe refreshing the picture on the first monitor would (somehow) cause the second monitor picture to change. Yeap, that did the trick:

tell application "Finder"

tell application "System Events"
set theDesktops to a reference to every desktop
set picture of item 2 of the theDesktops to file path_to_file_2
end tell

set desktop picture to file path_to_file_1
end tell

The key, though a bit unusual, is to set the Desktop Picture for the second monitor first, and only AFTER that, set the main screen Desktop Picture. As a side note, I should add that the path_to_file_1 and path_to_file_2 were in POSIX format and I’ve used the following code for that:

set path_to_file1 to POSIX file "/Users/myusername/Desktop/file1.jpg"
set path_to_file2 to POSIX file "/Users/myusername/Desktop/file2.jpg"

Be Sociable, Share!

Comments

1

Mark Kyprianou

try:

tell application “System Events”
tell desktop %d to set picture to POSIX file “%s”
end tell

and substitute the display (1 or 2) as the first arg and the file name as the second.

Leave your comment:
XHTML:You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> . * required