Sunday, July 18, 2010

Using PlistBudder to Set ENV Variables ...

My development platform of choice is Mac OS X. Here I share what I recently learned about the use of PlistBuddy as related to setting system env variables which any application, such as the Mac Terminal application, Safari, or any other Mac GUI application, will have access to. This is different than the use of say .bash_profile, which provides only those applications that are started from a terminal window, access to the various env variables you might have setup there.

Firstly, PlistBuddy is a command-line tool, located in /usr/libexec. This tool supports the editing of Mac plist files. The plist file that you can setup to include env variables should be located in $HOME/.MacOSX/environment.plist. This is not a default file so you'll have to create it. PlistBuddy is a great place to start.

Some points I learned the hard way on the use of PlistBuddy and the $HOME/MacOSX/environment.plist file.

  1. If you include you HOME env in $HOME/.MacOSX/environment.plist make sure it is correct. The reason is that should you set it to the incorrect directory then applicants that store important files in your $HOME directory with not be able to find them and may fail to work altogether. Safari for example will likely start to display a dialog on its startup that reports it is unable to locate your keychain. (Note: that setting up your $HOME env here is not really needed as it is setup already for you by other system scripts when you login. I did because that little bit of information I was not aware of at the time.)
  2. You can't use variable shorthand, such as $HOME, within the values of the env keys you will likely create inside of $HOME/.MacOSX/environment.plist. I didn't like this but such is the case. This means a great deal more work in setting up long variables such as a Java CLASSPATH env. 
  3. If you also add an identical env in our .bash_profile file that variable will overwrite or hide the one in $HOME/.MacOSX/environment.plist. So be carefull.
To use PlistBuddy follow these simple steps:
  • Startup the Mac Terminal app.
  • Start PlistBuddy up via the following command-line:
    • /usr/libexec/PlistBuddy ~/.MacOSX/environment.plist -- If the file does not already exist than PlistBuddy will create it.
  • To see the applications help enter 'help' at the PlistBuddy command line prompt.
  • To 'Add' a Key/Value pair use the 'Add' command as in:
    • Add :JAVA_HOME string /Library/Java/Home
  • To 'Delete' the same key use this command:
    • Delete JAVA_HOME
  • To view a key's value use 'Print' is in:
    • Print JAVA_HOME
Here is a very good PDF on the use of various unix tools available on the Mac OS X platform. Included within this PDF is some coverage on the use of PlistBuddy and another tool you will also likely find use for is 'plutil'.

Enjoy,
Philip