How do I use "defaults write" correctly from pkgbuild / productbuild?

1.2k views Asked by At

I'm building an installer package with

pkgbuild --component MyApp.app ... --scripts scriptsdir MyApp.pkg

And I would like to set some user defaults after installation. So I made a postinstall script with:

defaults write com.mycompany.MyApp key -string value

However, this doesn't work — the settings get written to /var/root/Library/Preferences, and are only accessible by sudo defaults read.

So instead I tried this:

defaults write ~/Library/Preferences/com.mycompany.MyApp key -string value

Now they get written to the right place, but the permissions are wrong — stat shows the permissions on the plist file are -rw------- 1 root wheel, and defaults read can't access them. How can I get this to work correctly?

1

There are 1 answers

2
gneagle On

Don't do this.

If your app requires certain "default" defaults to operate, on first launch, notice they aren't there and write them out.

You are assuming too much about the installation: that the user doing the install is the primary user of the software; that only one user on the machine might be interested in using the software; that anyone is logged in at all!

Instead have your app set up its default environment on first launch. This will work for all users on the machine, including users created after your software is installed.