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"

Set APOD and AAPOD as your Desktop Pictures

November 10, 2009 19:36 by scibuff

Download my APOD AAPOD Desktop application.

Some time ago I wrote a little batch script that downloads the most recent Astronomy Picture Of Day (APOD) and sets it as your desktop wallpaper (on a Windows system). This time, I’ve improved on it a bit. First of all, it’s a for OSX (tested on OSX 10.5.8). But the major improvement is that apart from an APOD image, it also downloads the Amateur Astronomy Picture Of Day (AAPOD) which it then sets as the Desktop Picture on my second screen.

Unlike for the Windows app, there is no need for any additional libraries (such as wget, grep, etc for win32) as a typical OSX installation has all necessary tools (curl and perl are required, but should be installed under the majority of installation settings). The main script, wallpaper.pl, that does all the hard work is written in Perl using its powerful text processing capabilities. The other core file, named setdesktop.sh, is just a simple applescript (executed via shell) that sets the Desktop Pictures for my two screens.

The wallpaper.pl is based on Harold Bakker’s APOD script. I have added some command line options and the bits to get the AAPOD image. By default, all images, both APOD and AAPOD, will be stored within the images directory (although this can be disabled via a command line option). When you unpack the application archive there will be two items. One is the apod_aapod.zip containing the necessary files and the other is the install.command*.

The install.command is a simple shell script that sets everything up. It unpacks the contents of the apod_aapod.zip into the /Applications/APOD_AAPOD/ directory (which it creates if the directory doesn’t exists). During the installation the APOD_AAPOD.app application is created (by compiling plain AppleScript using osacompile). Finally, the script will add the newly created application to your Login Items so that your desktop pictures are updated every time you log in.

In the default setting, the application is meant to work with two monitors system. The APOD image will be set as the desktop picture on the main screen and the AAPOD will be set as the desktop picture on the second screen. To modify this behavior is fairly easy:

  1. Simply navigate to /Applications/APOD_AAPOD_Desktop/ directory and open the APOD_AAPOD.sh file.
  2. Scroll all the way down pass the comments to the last line that reads: perl wallpaper.pl -s -m 0 -n 2
  3. Read more about the available flags and what they affect in the few lines above (in the end of the comments under “Usage”):
    • – to reverse the APOD and AAPOD screens, i.e have the AAPOD image be set as the desktop picture on your main monitor, just add the ‘-r’ option, so that the line reads perl wallpaper.pl -s -m 0 -n 2 -r
    • – to prevent the application from storing the downloaded images, simply remove the ‘-s’ option
    • – If you have only a SINGLE monitor, you should change the value following the ‘-n’ option to ‘1’.
    • – If would like to only download the APOD or only the AAPOS image, change the value after ‘-m’ to 1 or 2, respectively.

To apply the changes, either wait until the next time you log, or run the “APOD_AAPOD” application.

* Note: The only reason is has the .command extension instead of the typical .sh is that so it can be executed by double-clicking on the file. If you prefer to run in via terminal, simply rename it to install.sh and run it via sh install.sh.