Applescript to change desktop image on all monitors?

I have the following code to change the desktop image of my Macbook

tell application "System Events"
tell current desktop
set picture to "/Library/Desktop Pictures/Beach.jpg"
end tell
end tell

How do change all desktops - I have a second monitor.


The previous answers look great, but I wanted to share a way I found to do this with much less code.

tell application "System Events"
    tell every desktop
        set picture to "path/to/picture.png"
    end tell
end tell

You could even condense it to one line:

tell application "System Events" to tell every desktop to set picture to "path/to/picture.png"

Here's a little script I use to set most of the Desktop Background properties for my dual monitors:

tell application "System Events"
    tell desktop 1
        set pictures folder to "/Library/Desktop Pictures"
        set picture rotation to 2 -- using interval
        set change interval to 1800
        set random order to true
    end tell
    tell desktop 2
        set pictures folder to "/Library/Desktop Pictures/Mine"
        set picture rotation to 2 -- using interval
        set change interval to 1800
        set random order to true
    end tell
end tell

I use this because MacOSX often resets my backgrounds to the default.


This might work. I only have one monitor so couldn't properly test it.

tell application "System Events"
    set desktopCount to count of desktops
    repeat with desktopNumber from 1 to desktopCount
        tell desktop desktopNumber
            set picture to "/Library/Desktop Pictures/Beach.jpg"
        end tell
    end repeat
end tell