AppleScript – Get every desktop including hidden Mission Control desktops

applescriptdesktopmission-controlwallpaper

I'm trying to write a simple script that will change my desktop background on all of my desktops/workspaces at once. Right now I have this:

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

This will change the wallpaper on both of my monitors for the desktop that is visible, but doesn't change the wallpaper on all of the 'hidden' desktops in Mission Control (Desktops 2-4 for example). Is there some way to modify this to enact on all desktops?

Best Answer

The following is a start until I figure out how to read the desktop count in Mission Control. Right now for this to work you must be on the farthest left Desktop.

Not knowing where your pic is located I'm targeting an image in Desktop Pictures with:

set thePic to file ((path to library folder from local domain as text) & "Desktop Pictures:Blue Pond.jpg") as text

I set a variable for the count of Mission Control Desktops:

set windowCount to 5

I build a repeat loop starting with the first number till it reaches the count:

repeat with X from 1 to windowCount

end repeat

I use key code to move right with control only when the count is greater than 1 and a delay:

repeat with X from 1 to windowCount
    tell application "Finder"
        tell application "System Events"
            if X > 1 then
                key code 124 using control down ## move right
                delay 1
            end if
        end tell
    end tell
end repeat

I then reference every Desktop and add a delay on the picture to be set:

tell application "System Events"
    set deskGroup to (a reference to every desktop)
    repeat with theDesk in deskGroup
        set picture of theDesk to thePic
    end repeat
end tell
delay 1

the complete code:

set thePic to file ((path to library folder from local domain as text) & "Desktop Pictures:Blue Pond.jpg") as text
set windowCount to 5
repeat with X from 1 to windowCount
    tell application "Finder"
        tell application "System Events"
            if X > 1 then
                key code 124 using control down ## move right
                delay 1
            end if
            set deskGroup to (a reference to every desktop)
            repeat with theDesk in deskGroup
                set picture of theDesk to thePic
            end repeat
        end tell
        delay 1
    end tell
end repeat

this is one way to do this hope it helps. This works and was tested using macOS High Sierra 10.13.6