How do you set environment variables in OSX

Answered

Comments

4 comments

  • Avatar
    juanjo

    Can you paste the code you are using to add the variable? In which OS X version are you testing it? Is you application (the one that expects some variables to be defined, not the installer) an application bundle or a binary?

  • Avatar
    FSNB

    Hello,

    I use

    <addEnvironmentVariable>
       <name>NEWPATH</name>
       <scope>system</scope>
       <value>${installdir}</value>
    </addEnvironmentVariable>
    

    This same code works on the windows release.

    I am building my installer in windows, and installing it on OSX 10.8.2, I am using Installbuilder Pro 8.5.1

    The application that would use the variable is a .app bundle installed with the installbuilder tools. It is started from the Dock.

    The environment variable does work in the Terminal utility, and if I use "open /Applications/.../myapp.app" from the terminal, the environment variable is available. The only failure is when the application is started by clicking the the dock or with spotlight.

  • Avatar
    FSNB

    Okay, I have figured out that I need to modify my App bundle's info.plist xml file. So the new question is, how do I add a new value to an XML file?

    For example,

    ?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
        <dict>
            <key>CFBundleGetInfoString</key>
                <string>Copyright IBM Corp. and others 2002, 2010. All rights reserved.</string>
            <key>CFBundleIdentifier</key>
                <string>org.myAPp</string>
        </dict>
    </plist>
    

    This file is going to need the ${installDir} installbuilder variable. How would I add an additional key value within the dict key using installbuilder?

  • Avatar
    juanjo

    If you are not already defining the LSEnvironment key, you could use the below (please note you should skip the .plist extension):

      defaults write yourApp.app/Contents/Info LSEnvironment '<dict><key>MY_ENV_VAR</key><string>MY_ENV_VALUE</string></dict>' 
      plutil -convert xml1 yourApp.app/Contents/Info.plist
    

    Using a runProgram:

    <runProgram program="defaults">
       <programArguments><![CDATA[write yourApp.app/Contents/Info LSEnvironment '<dict><key>MY_ENV_VAR</key><string>MY_ENV_VALUE</string></dict>']]></programArgument>
    </runProgram>
    <runProgram program="plutil">
       <programArguments>-convert xml1 yourApp.app/Contents/Info.plist</programArgument>
    </runProgram>
    

    Or you could simply include a placeholder in you app Info.plist and substitute it using the <substitute> action.

Please sign in to leave a comment.