Last Week on My Mac: Obsolescent preferences

If I’m ever short of a good item for a Q&A I can always rely on preferences. There’s no other topic dating right back to the first release of Mac OS X which is so versatile in its ill-effects. Apps crashing or failing to open at all, weird happenings in the Finder, soaring CPU load, or almost any sign of trouble and you can almost invariably find a cause stemming back to those property lists in a Preferences folder.

Last week they came up in the context of the bug I’m experiencing in the Finder, which occasionally forgets my main working folder and has to be restarted to restore its pride of place in my Finder windows. “Trash” was the good suggestion, followed quickly by the reminder that it’s better to use the defaults command tool instead. But why on earth should a Mac user have to resort to Terminal to try a popular remedy for a commonplace type of problem? After twenty years of evolution and revolution, hasn’t macOS advanced at all?

No. If anything, in this respect it has grown worse.

In this era of absent documentation, we should be grateful that Apple’s developer reference on preferences is at least fairly complete, if paradoxical. What’s known to the coder as UserDefaults has little regard for the user, and is all about customisation rather than defaults. Apparently it’s “an interface to the user’s defaults database, where you store key-value pairs persistently across launches of your app.”

No sooner does Apple tell you that, than it’s issuing you a warning:
Don’t try to access the preferences subsystem directly. Modifying preference property list files may result in loss of changes, delay of reflecting changes, and app crashes. To configure preferences, use the defaults command-line utility in macOS instead.” The sad fact is that UserDefaults and its preference files don’t have any common controls or utilities beyond the command shell interface. Instead, macOS manages them with an opaque service cfprefsd, which isn’t remotely friendly, even to those comfortable in Terminal.

Return to my example of the Finder, which thankfully seldom has serious problems with its preferences. When it does, though, macOS has painted you into a corner. Because the Finder is running all the time except when it’s starting up and about to run,* its preference settings are constantly held in memory by the dreaded cfprefsd, and changes a user might make to its preference file can always be overwritten.

The Finder doesn’t provide the user any control to reset its preferences to factory defaults either. Without that, and knowing that it’s pointless trying to do anything to the preference file itself, all the user can do is open Terminal, type
defaults delete
quickly force the Finder to be restarted, and hope that cfprefsd hasn’t done anything contrary in the meantime.

There might be signs of promise in UserDefaults, though, which has a method resetStandardUserDefaults(), which looks as if it could be used to restore user defaults. Except that Apple warns that “this method has no effect and shouldn’t be used.”

Just to run that past you again, UserDefaults doesn’t help the user here, and can’t even restore defaults. That must be why it bears its name.

I haven’t heard of it happening for some years now, but in the past I’ve had several questions submitted by readers whose Macs have been temporarily rendered unusable because Finder’s preferences have become sufficiently damaged that they keep crashing the Finder, which is promptly restarted to continue a seemingly endless cycle.

Few Mac developers can’t at some stage have wanted to abandon Jekyll UserDefaults and Hyde cfprefsd in favour of their own system of preference settings. For me that moment first came when the app I was trying to develop had a bug which wrote one value incorrectly in its preference file. I was unable to run that app again until I had located the error in the existing preference file, as each time I tried to trash that, cfprefsd seemed able to restore it, complete with its corrupted value.

Yet all that appears to have happened in recent years is that those hundreds of preference files which used to be in ~/Library/Preferences are slowly scattering to the four winds, including iCloud. cfprefsd seems, if anything, to be increasingly retentive. What we’ve ended up with is like an amorphous database distributed across hundreds of files which we can only control by typing in raw SQL.

Just like startup modes on Intel Macs, the time has come for Apple to design a new, coherent, user-centred replacement for UserDefaults – this time, something which empowers the user and caters for preferences, please.

* Thanks to Jeff Johnson @lapcatsoftware for pointing out that you can quit the Finder in Activity Monitor without it being automatically restarted. You can also enter in Terminal

defaults write QuitMenuItem -bool YES

to reveal the Finder’s hidden Quit menu command, to save you from having to do that in Activity Monitor.