I wrote a piece of Applescript that changes the wallpaper on macOS. I usually have multiple virtual desktops (spaces) and I'd like to manage all of them. While it seems to be easy to control physical desktops (i.e. on a different monitor), I can't find a way to do the same for virtual desktops. Of course I did some research before posting and, as far as I see, there are only unsatisfied questions, at least up to 2022. Can somebody confirm nothing has changed in the past year?

PS I've seen utilities written in other languages (e.g. https://github.com/georgesofianosgr/wally) that seem to do the job, but I see that they are reverse-engineering hacks directly manipulating system databases, so I prefer not to use them.

1 Answer 1


I'm not aware of a single AppleScript statement that can achieve that (snippets that appear in Applescript to change desktop image on all monitors? don't change inactive virtual desktops). What I can recommend is writing a cron setup that will update the wallpaper for current desktops.

What I do is, I've setup a config file $XDG_CONFIG_HOME/wallpaper that holds the path to my desired wallpaper, e.g., /Users/grzesiek/Pictures/Wallpapers/tokyo-morning.jpg.

Then, I've created a script that continously sets the wallpaper using AppleScript:

set -l start (date +%s)
for i in (seq 1 60)
    # Quit after 60 seconds. This is to prevent the script from potentially
    # overlapping with the next cron run.
    if test (date +%s) -gt (math $start + 60)
        exit 0

    if test -f $XDG_CONFIG_HOME/wallpaper
        set wallpaper (cat $XDG_CONFIG_HOME/wallpaper)
        osascript -e 'tell application "System Events" to tell every desktop to set picture to "'$wallpaper'"'

    sleep 1

Then, I've added that script to my crontab:

* * * * * fish /Users/grzesiek/.local/bin/update-mac-wallpaper

As a result, every desktop that I happen to use gets updated within a second.

  • Yes, gregorias, it's precisely what I did, that's why I asked the question: the limitation is that it sets the same wallpaper to all virtual desktops. Commented Apr 10, 2023 at 20:53

You must log in to answer this question.

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