Change Mac GUI app environment variable

Application launched from finder on mac does not respect environment variables set by bash. Here are some methods to set environment variables for GUI applications.

Simplest method: modify via bash

NODEENV=development ./trader.app/Contents/MacOS/Electron 

Global method:

launchctl setenv NODE_ENV development

More customizable: modify plist file

  1. Open the app by right clicking Show Package Content
  2. Edit Info.plist file. Add following section
  3. Restart Finder by killall Finder in terminal.app
  <key>LSEnvironment</key>
  <dict>
    <key>NODE_ENV</key>
    <string>development</string>
  </dict>

A caveat here is: Editing this file only have effect before the app is launched the first time. Then launch service is going to cache this. Use this command for update to take effect.

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f myapp.app

or this?

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user 
killall Finder