How to use the defaults command with App Groups

defaultsterminal

In OS X Yosemite it's possible to have preferences that are shared between multiple apps that are in the same App Group. On the development side of things it's as simple as setting up the App Group and then using [[NSUserDefaults alloc] initWithSuiteName:@"TEAMID.com.company.AppGroup"] instead of the usual [NSUserDefaults standardUserDefaults]. These preferences get stored in ~/Library/Group\ Containers/TEAMID.com.company.AppGroup/Library/Preferences/TEAMID.com.company.AppGroup.plist.

What I'm trying to figure out is how to modify those preferences from outside of the app using the defaults command. So far the best I've found is this:

defaults write ~/Library/Group\ Containers/TEAMID.com.company.AppGroup/Library/Preferences/TEAMID.com.company.AppGroup PreferenceName -bool YES

This sort of works in that it modifies the correct .plist file and updates the preferences I tell it to. But it doesn't seem to be compatible with the caching that's used in Yosemite. As soon as the app tries to read the same preference, it reverts back to what was set previously.

Is there any way to make this work reliably?

Best Answer

As far as I've been able to determine there's still no proper way to do this. However, you can make it work by killing cfprefsd after using the above command. So I just use something like this in the Terminal app:

defaults write ~/Library/Group\ Containers/TEAMID.com.company.AppGroup/Library/Preferences/TEAMID.com.company.AppGroup PreferenceName -bool YES; killall cfprefsd

Not great, but better than nothing.

Update for macOS Sierra (10.12): Unfortunately using the file path doesn't seem to work anymore. Strangely it works on files that are elsewhere—like if I copy an App Group plist to the desktop—but I can't get it to work right on an App Group plist in its normal location.

It's worth noting that man defaults has included this note for a while:

The defaults command will be changed in an upcoming major release to only operate on preferences domains. General plist manipulation utilities will be folded into a different command-line program.

Like I said though, this isn't an issue with arbitrary plists, it seems specific to plists in ~/Library/Group Containers/.