1

I am looking for a solution to create a binary plist file from the command line on OS X?

I want to create the ~/.MacOSX/environment.plist and setup the PATH there.

I know that this plist file has to be in the binary format and I know how to write to it using the defaults write command. Still, I do need to create the file before doing this, and creating an empty file does not work.

Note: I need a command line solution.

7

You could also create an XML or old-style property list and convert it with plutil:

plutil -convert binary1 test.plist

It modifies files in place by default. You can specify a different output file with -o or print to stdout with -o -.

1

At least as early as Mac High Sierra (10.13.4), defaults write (e.g., whether of the form defaults write com.example.test '{a = (b, 2);}' or defaults write com.example.testb "a" '(b, 2)') is creating binary plist files for me in ~/Library/Preferences/ even when the file hasn't yet been created.

And it can be created at a particular path by swapping a domain with the desired file path, e.g.,

defaults write "/users/brett/Desktop/com.example.testc"  "a" '(b, 2)'

The XML syntax also works:

defaults write "/users/brett/Desktop/com.example.testc" '<dict><key>a</key><array><string>b</string><string>2</string></array></dict>'

(Supplying JSON does not work (despite plist being able to convert it to JSON).)

One can also use defaults import to create a new binary plist file, whether defaults import <domain> - which lets you (via stdin) use either of the syntaxes above (or defaults import <domain> pathToExistingPlist, assuming the referenced plist is itself a binary file).

Finally, one can use defaults export on an existing file, assuming it is expressed as XML or the ASCII property list (or another binary plist).

defaults export com.example.test1 /path/for/com.example.testcopy.plist

The latter is similar to the behavior of the plutil command in the answer by @user495470 except that it will only copy in place if you specify a path in the second argument which leads to the same file as that referenced in the first (domain).

With the exception of the latter export syntax (which creates an empty <dict> upon finding a non-dict type at root), for any of the above, errors are given when trying anything but a dict at root.

-2

This piece of bash solved my problem:

if [ ! -f '~/.MacOSX/environment.plist' ]; then
  mkdir -p '~/.MacOSX'
  defaults write ~/.MacOSX/environment.plist TEST_VAR 1
fi

Still, I have to warn others that OS X 10.8 seems not to be loading the file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .