Setting the wallpaper on macOS

Mon Jun 17 2019Setting the wallpaper for all spaces automatically is surprisingly hard.

Written by Tilman Roeder

When trying to set up a bash script to update the wallpaper on my Mac, I ran into this wired issue. Basically, the wallpapers of each space / desktop are really smart and change independently 🤪. So after some frantic Internet searches, I found out the following:

  • The different spaces seem to be managed by the Dock (?, at least their stuff is stored in ~/Library/Application\ Support/Dock/)
  • Which picture each desktop should have is stored in an SQLite database file
  • The image paths are stored in the data table (the relevant column is value)
  • To apply the changes, you have to restart the Dock
  • If you want to change to also apply to different monitors and behind your fullscreen windows, there is a neat little AppleScript for that:

TL;DR

function background {
  IMG=$1
  osascript -e "tell application \"System Events\" to set picture of every desktop to \"${IMG}\""
  sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '${IMG}'"
  killall Dock
}

Let's hope this post will save someone like me a few searches and head-scratches 😅. By the way, I use Unsplash to download the fresh backgrounds, and have amassed a nice little collection of suitable pictures.