Setting environment variables via launchd.conf no longer works in OS X Yosemite/El Capitan/macOS Sierra/Mojave?

前端 未结 9 1222
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 08:07

It looks like the launchd.conf does not load my environment variable anymore. Has anyone else noticed that?

Is there another solution to permanently set

9条回答
  •  一向
    一向 (楼主)
    2020-11-22 08:15

    [Original answer]: You can still use launchctl setenv variablename value to set a variable so that is picked up by all applications (graphical applications started via the Dock or Spotlight, in addition to those started via the terminal).

    Obviously you will not want to do this every time you login.

    [Edit]: To avoid this, launch AppleScript Editor, enter a command like this:

    do shell script "launchctl setenv variablename value"
    

    (Use multiple lines if you want to set multiple variables)

    Now save (+s) as File format: Application. Finally open System SettingsUsers & GroupsLogin Items and add your new application.

    [Original answer]: To work around this place all the variables you wish to define in a short shell script, then have a look at this previous answer about how to run a script on MacOS login. That way the the script will be invoked when the user logs in.

    [Edit]: Neither solution is perfect as the variables will only be set for that specific user but I am hoping/guessing that may be all you require.

    If you do have multiple users you could either manually set a Login Item for each of them or place a copy of com.user.loginscript.plist in each of their local Library/LaunchAgents directories, pointing at the same shell script.

    Granted, neither of these workarounds is as convenient as /etc/launchd.conf.

    [Further Edit]: A user below mentions that this did not work for him. However I have tested on multiple Yosemite machines and it does work for me. If you are having a problem, remember that you will need to restart applications for this to take effect. Additionally if you set variables in the terminal via ~/.profile or ~/.bash_profile, they will override things set via launchctl setenv for applications started from the shell.

提交回复
热议问题