How can I script the setup of preferences in Mac OS X?

I have a couple of Macs and I would like to create some kind of script that I can use to:

  1. Easily setup a new Mac after a reinstall
  2. Update settings to consistent values (so if I decide to change some preferences on Mac A, I can run the script on Mac B to get it to match)

I'd be happy if I could use this script to customize:

  • System Preferences
  • Finder Preferences

I would prefer a method where I can use a script or config file to specifically set certain settings I choose to certain values. I don't really want something that will import/export ALL settings indiscriminately.

I also don't want some complicated IT management solution, since this is really just for me.


Solution 1:

You should take a look at Mathias Bynens' Dotfiles. Modify it for your needs and delete the settings you don't need. Then you only need to execute the script with the terminal.app via entering sudo /path/to/the/script/./osx on each mac.

Solution 2:

I did some more digging and I managed to find a solution that isn't AppleScript-ed UI movements.

You can script most of the settings changes I wanted to make using the defaults command (wiki).

As for figuring out the actual keys/values that correspond to the UI preferences, I've found lists like this to be useful (this one too). I've also used this little script I wrote to suss out the changes made by the UI:

#!/bin/sh
mv ~/new-defaults.txt ~/old-defaults.txt
defaults read > ~/new-defaults.txt
diff old-defaults.txt new-defaults.txt

It'll give you all the changes to the defaults since the last time it was ran. There's likely going to be other changes detected (like window positions), but most of the keys seemed reasonably named. You can then take the changed key and figure out its domain by searching the full file.

There appear to be other mechanisms for storing preferences in Mac OS besides this (e.g. allowing non-App Store apps does not seem to be reflected in the defaults plists), and I still haven't figured those out. The list I liked above uses some other commands, and I'll have to look into those next.