brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: diff two defaults read dumps to find the key behind a Mac setting

Terminal
~/tips/tip-macos-defaults-read-diff

defaults read > ~/before.txt

Shows which preference key a setting changes, so you can script it later.

Half the fun of the defaults command is finding out which key a checkbox in System Settings or in an app actually writes. Guessing from old blog posts works sometimes. I prefer to let the Mac tell me.

Without a domain, defaults read prints every preference domain of your user. So you take a snapshot, flip the setting, take a second snapshot and compare the two:

defaults read > ~/before.txt
# now change the setting in System Settings or in the app
defaults read > ~/after.txt
diff ~/before.txt ~/after.txt

The diff shows the domain and the key that changed, plus the new value. Say you switch an option in TextEdit’s settings: you’d see a key appear or change under com.apple.TextEdit, and from then on you can set it from a script with defaults write on a new Mac.

Some settings are stored per machine instead of per user. If the first diff is empty, repeat the trick with the -currentHost option, which limits defaults to the host you are logged in on:

defaults -currentHost read > ~/before-host.txt

The caveat: an empty diff does not always mean you did something wrong. Not every setting lives in your user defaults, and some only land on disk after the app writes them a moment later, so give it a few seconds before the second dump. The man page also warns that a running app may not see a change you write and can even overwrite it, so quit the app before you write the value back. For Dock and Finder, killall Dock or killall Finder makes them read it again.

I’d delete the two text files afterwards. They hold every preference you have, recent file names included, and that is not something I’d leave lying around or paste in a forum.

Further reading: defaults man page (ss64)

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link