Desktop background on OS X 10.9 Mavericks

Published
2013-11-02
Tagged

I upgraded to OS X 10.9 a week or so ago, along with everyone else in the Mac nerd world. And like everyone else in the Mac nerd world, something of mine broke. In my case, it was a small script that would alter com.apple.desktop.plist to daily change my desktop.

Essentially, you have no way in OS X of syncing desktop backgrounds between spaces. You can set desktops so that all your spaces have the same desktop, but if you want to cycle them randomly, the first time the OS changes them, they’ll be all different once again. The problem is not a huge one, but it fell into a particular category of being big enough to annoy me, and small enough that I could write a script to fix it.

Under OS X Lion (and before) these preferences were stored in ~/Library/Preferences/com.apple.desktop.plist, and my original script would open this up and edit values for each space. However, as soon as I moved to Mavericks, this broke. The script would continue to edit the plist file, but the desktop wouldn’t change in response.

The internet was of no help - I imagine this problem is pretty niche - so I ended up poking around in the guts of ~/Library/ to work out where OS X now stored desktop preferences. After several sessions of searching, I came across a promising file: ~/Application Support/Dock/desktoppicture.db. This is an SQLite database file, and poking around in its innards, it definitely seems to be linked to desktop background preferences:

1
> sqlite3 desktoppicture.db 
2
SQLite version 3.7.13 2012-07-17 17:46:21
3
Enter ".help" for instructions
4
Enter SQL statements terminated with a ";"
5
sqlite> .tables
6
data         displays     pictures     preferences  prefs        spaces     
7
sqlite> SELECT * FROM data;
8
~/Archive/Wallpapers/SimpleD
9
~/Archive/Wallpapers/SimpleD/code.png
10
~/Archive/Wallpapers/Merek
11
~/Archive/Wallpapers/Merek/Purple_Rain-Retina.jpg
12
~/Archive/Wallpapers/SimpleD/old-fashioned.png

At first, some of the tables look a bit cryptic:

1
sqlite> .header ON
2
sqlite> SELECT * FROM preferences;
3
key|data_id|picture_id
4
1|5|8
5
10|4|8
6
10|4|9
7
1|5|9
8
1|5|10
9
10|4|10
10
10|4|11
11
1|5|11
12
1|7|2
13
10|6|2
14
10|6|3
15
1|7|3
16
1|7|14
17
10|6|14
18
10|6|15
19
1|7|15
20
1|5|12
21
10|4|12
22
10|4|13
23
1|5|13
24
10|6|4
25
1|7|4
26
1|11|5
27
10|4|5
28
10|4|6
29
1|11|6
30
10|4|7
31
1|11|7
32
10|4|1
33
1|11|1

However, after a bit of investigation I pieced together some structure to them:

desktoppicture.db table layout. Arrows indicate references to other tables. I never took a course in proper UML.

This is how the tables relate:

  • data stores a series of files and folders, which represent either desktop backgrounds or folders of images.
  • displays is a list of the computer displays, each of which is given a UUID to differentiate it from other displays.
  • spaces is a list of spaces currently operating on the computer. Again, each gets a UUID except for one: the primary space has no UUID.
  • pictures is a somewhat badly-named table. Each entry in pictures links a display and a space, although both references can also be null. I’m not sure why this table exists, since I’d expect you could more easily link each space to its display. Regardless, this table has a list of space-display relationships.
  • preferences is a list of relationships between entries in the data table (i.e. folder and file paths) and entries in the pictures table (i.e. space/display combinations). Each entry also has an integer value key. It appears that if key is 1, the entry indicates a background image, and if the key it 10, the entry indicates a folder. Presumably this is for OS-mandated background-changing algorithms.
  • prefs is a somewhat unrelated table, in which I assume the system stores information about whether to change desktop backgrounds regularly.

While this is educational, the main thing we learn is that we hardly have to touch any of the database. All we need to do is extract the filenames from the data table, use them to pick new file names, and then update the table. Here’s the full ruby code to do this:

1
#!/usr/bin/env ruby
2
3
require "sqlite3"
4
5
DB_LOC = File.join(ENV["HOME"], "Library/Application Support/Dock/desktoppicture.db")
6
DB = SQLite3::Database.new(DB_LOC)
7
8
DB.execute("SELECT ROWID,value FROM data").each do |(rowid,path)|
9
  path = File.expand_path(path)
10
  next if File.directory?(path)
11
12
  image_dir = File.dirname(path)
13
  new_image = Dir[File.join(image_dir,"*")].sort_by{ rand }[0]
14
  new_image.sub!(ENV["HOME"],"~")
15
16
  DB.execute("UPDATE data SET value=? WHERE ROWID=?",[new_image,rowid])  
17
end
18
19
`killall Dock`

We need to use File::expand_path because the database quite happily encodes your home directory as ~, which ruby doesn’t like. Similarly, we sub the home directory out at the end to put it in a form the system will like. We could probably get away with leaving the full path in there, but better safe than sorry. Finally, we need to run killall to restart the dock: it looks like otherwise the deskop won’t change background.

So there we go. It took a while to work out what was wrong, but Apple’s replacement desktop background file is surprisingly easy to edit.