Set environment variable for the process before startup

后端 未结 2 843
轻奢々
轻奢々 2021-01-15 18:14

I have the following situation:

I have Mac OS bundle with application which uses some 3rd party dynamic libraries and those libraries depend on some environment vari

2条回答
  •  伪装坚强ぢ
    2021-01-15 18:50

    You can add a key "LSEnvironment" to your app bundle's Info.plist. The value can be a dictionary with strings for keys and values and those key-value pairs will be added to the environment when your app is launched by Launch Services (e.g. from the Finder or Dock but not from the Terminal).

    LSEnvironment
    
        ENV_VAR
        value
    
    

    However, in my testing (on Snow Leopard), it was a bit flaky to test, at least when editing the Info.plist of an existing app. Basically, Launch Services caches this part of the app's Info.plist when it first encounters the app and won't necessarily recognize changes on disk. You can sometimes prompt it to reread the Info.plist by, for example, duplicating the app bundle or temporarily moving it to a different folder. Of course, the overkill solution would be to use lsregister to flush and rebuild the cache:

    /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -seed
    

    This caching issue won't affect your end users, just you as you tweak the Info.plist. Also, it shouldn't affect you if you make the change in your source Info.plist and then build the app using Xcode.

提交回复
热议问题