3

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?

1 Answer 1

1

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

4
  • Thanks, and I haven't tried this just yet, but how is this different from the last code block I had written in my question? May 11, 2016 at 3:18
  • The way I generate the path string looked a little different than yours. There's actually no way to manipulate the desktop backgrounds of "background desktops" (desktops that are not currently shown on screen) without utilizing accessibility features within the OS. May 11, 2016 at 12:22
  • Do you have an example utilizing said features? I would be very interested in knowing how to do that. Jul 5, 2016 at 21:24
  • @chrissphinx I haven't tried this method, but here's a simple example: discussions.apple.com/thread/5670099?tstart=0 Jul 7, 2016 at 11:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .