Seamlessly change all desktop spaces' wallpaper on Mac without killall Dock

I would like to change the wallpaper on ALL desktops including spaces on Mac but without needing to call killall Dock each minute. (Dock restarting forces wallpaper refresh).

I have an AppleScript that changes the desktop wallpaper instantly:

tell application "System Events" to tell every desktop to set picture to wallpaperPath

but that only changes the wallpaper on the active space (meaning that if the main desktop is not active, its background will not change).

I found this question https://stackoverflow.com/questions/36185506/how-to-loop-through-all-mac-desktop-spaces which suggests updating wallpaper path values in the SQLite database located at ~/Library/Application Support/Dock/desktoppicture.db. This changes the wallpaper at every space which is great but it requires restart of the dock using killall Dock which is undesirable as it disrupts the workflow.

My question is - is there some way to combine these two approaches? Seamlessly change wallpapers on every desktop space?

Any suggestions would be appreciated. I have no preferred language, it can be in C, Swift, Python, Bash, AppleScript etc.


Use NSWorkspace and the setDesktopImageURL:forScreen:options:error: method:

setDesktopImageURL:forScreen:options:error:

Sets the desktop image for the given screen to the image at the specified URL.

This API is accessible through Objective-C and Swift. I presume scripting bridges for python can also access these methods.

See the links below for other approaches using AppleScript:

  • Changing the background image in all spaces
  • Can I have a custom desktop wallpaper 'Change Picture' interval?