Friday, January 18, 2013

Changing OSX Wallpapers on Multiple Monitors via Script

[UPDATED 05/18/2014: Fixed script for Mavericks compatibility!]

For whatever reason, I like to use different desktop wallpapers during the day than I do during the night. I feel like it helps my eyes focus better on the open window I am working in (and I hate working in  maximized/fullscreen apps).

The problem is that I use a 4 monitor setup, and setting each desktop's wallpaper manually twice a day is so ... manual!  So I developed a script to help me make those changes automatically. The special thing about this script it that it iterates over desktops and changes the wallpaper. This was actually kind of tricky to find information about. Most information I could find only really applied to single monitor setups, and while they changed my wallpaper on the main monitor, did nothing for the other monitors. After some research and tweaking I finally came up with this, which seems to work well:

tell application "Finder"
 set path_to_file to "/Users/YourAccountName/Pictures/your-image.jpg"
 tell application "System Events"
  set theDesktops to a reference to every desktop
  repeat with aDesktop in theDesktops
   set picture of aDesktop to path_to_file
  end repeat
 end tell
end tell

Obviously (at least, hopefully so) you would modify the path_to_file assignment to an image that is interesting to you ... or you could handle an argument to the script from here, or query for a file, etc. etc.

The way I have this setup for my situation is that I have two copies of this script with different names and different images: One for night and one for day. This makes it easy to run them from a scheduler, or more typically, I call them from Alfred when I desire a change.

Anyways, hope you find that useful. Have fun!