MacOS – Can’t Set Desktop Background in Fullscreen App

applescriptdesktopmacos

I have an 11" Air so I usually run all of my open applications in fullscreen mode. I have a launchd job for my user that runs a script every hour and every login to set the desktop background according to time of day. The problem is that, despite running in the background, unless I am logging in or idling on the Desktop "space", it will not get set.

Weirdly, it also changes to dark mode at night (or light mode during daytime) and that part of the script works just fine.

So, if I go to my fullscreen'd Terminal.app and run the following lines, they do not work to change the desktop background:

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

or

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

or even

tell application "System Events"
    set desktopCount to count of desktops
    repeat with desktopNumber from 1 to desktopCount
        tell desktop desktopNumber
            set picture to "<path>"
        end tell
    end repeat
end tell

Does anyone know how to get this to work?

Best Answer

I'm late to the party, but this should work for you:

-- Set user_project_path to the folder that contains this AppleScript
set user_project_path to POSIX path of ((path to me as string) & "::")

-- Assuming your images are contained in user_project_path directory inside an images folder
set images_directory to user_project_path & "images/" as string

tell application "System Events"
  set desktopCount to count of desktops
  repeat with desktopNumber from 1 to desktopCount
    tell desktop desktopNumber
        -- Change my-desktop-image.png below to the name with extension of your desired image
        set picture to images_directory & "my-desktop-image.png"
    end tell
  end repeat
end tell

If you have questions, let me know