在 OS X 中为 GUI 应用程序设置环境变量

Per*_*ses 23 launchd environment-variables launchctl macos

如何在 Mac OS X 中设置环境变量,以便它们可用于 GUI 应用程序而不使用~/.MacOSX/environment.plist登录挂钩(因为这些已弃用)?

小智 22

在 Mountain Lion 上,所有/etc/paths/etc/launchd.conf编辑都没有任何效果!

Apple 的开发者论坛说:

“更改 .app 本身的 Info.plist 以包含具有所需环境变量的“LSEnvironment”字典。

~/.MacOSX/environment.plist 不再受支持。”

所以我直接编辑了应用程序Info.plist(右键单击“AppName.app”(在本例中为 SourceTree),然后单击“ Show package contents”)

显示包裹内容

并添加了一个新的键/字典对,称为:

<key>LSEnvironment</key>
<dict>
     <key>PATH</key>
     <string>/Users/flori/.rvm/gems/ruby-1.9.3-p362/bin:/Users/flori/.rvm/gems/ruby-1.9.3-p362@global/bin:/Users/flori/.rvm/rubies/ruby-1.9.3-p326/bin:/Users/flori/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:</string>
</dict>
Run Code Online (Sandbox Code Playgroud)

(请参阅:Apple 的 LaunchServicesKeys 文档

在此处输入图片说明

现在应用程序(在我的情况下为 SourceTree)使用给定的路径并与 git 1.9.3 一起使用 :-)

PS:当然,您必须根据您的特定路径需求调整 Path 条目。

  • 谢谢!这对我来说是完美的。在 10.11 (El Capitan) 上,我确实还必须运行 [Matthew 提供的命令](https://superuser.com/a/787415/583200) 才能看到我对 `Info.plist` 的更改生效。 (3认同)

Per*_*ses 9

该解决方案使用launchctl, 结合Launch Agent 的功能来模拟旧的登录挂钩。有关使用 store of 的其他解决方案launchd,请参阅此比较。这里使用的启动代理位于/Library/LaunchAgents/

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>user.conf.launchd</string>
    <key>Program</key>
    <string>/Users/Shared/conflaunchd.sh</string>
    <key>ProgramArguments</key>
    <array>
        <string>~/.conf.launchd</string>
    </array>
    <key>EnableGlobbing</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
    <key>LimitLoadToSessionType</key>
    <array>
        <string>Aqua</string>
        <string>StandardIO</string>
    </array>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

一件重要的事情是RunAtLoad键,以便尽可能早地执行启动代理。真正的工作是在 shell 脚本/Users/Shared/conflaunchd.sh 中完成的,它读取~/.conf.launchd并将其提供给launchctl

#! /bin/bash

#filename="$1"
filename="$HOME/.conf.launchd"

if [ ! -r "$filename" ]; then
    exit
fi

eval $(/usr/libexec/path_helper -s)

while read line; do
    # skip lines that only contain whitespace or a comment
    if [ ! -n "$line" -o `expr "$line" : '#'` -gt 0 ]; then continue; fi

    eval launchctl $line
done <"$filename"

exit 0
Run Code Online (Sandbox Code Playgroud)

注意的号召path_helper得到PATH树立正确的。最后,~/.conf.launchd看起来像这样

setenv PATH ~/Applications:"${PATH}"

setenv TEXINPUTS .:~/Documents/texmf//:
setenv BIBINPUTS .:~/Documents/texmf/bibtex//:
setenv BSTINPUTS .:~/Documents/texmf/bibtex//:

# Locale
setenv LANG en_US.UTF-8
Run Code Online (Sandbox Code Playgroud)

这些是launchctl命令,有关更多信息,请参阅其联机帮助页。对我来说很好用(我应该提到我仍然是一个雪豹人),像texstudioTeXShop这样的 GUI 应用程序可以看到我自己的 tex 树。可以改进的地方:

  1. shell 脚本中有一个#filename="$1"。这并非偶然,因为文件名应该由启动代理作为参数提供给脚本,但这是行不通的。

  2. 正如这里提到的(德语和付费墙后面!),可以将脚本放在启动代理本身中。

  3. 我不确定这个解决方案有多安全,因为它eval与用户提供的字符串一起使用。

  4. 我想记住使用这种方法定义 MANPATH 效果不佳,但我不确定。

应该提到的是,Apple 打算通过在?/launchd.conf 中放入一些类似的方法,但目前不支持此日期和操作系统(请参阅 的联机帮助页launchd.conf)。我想像通配这样的事情不会像他们在这个提案中那样工作。当然,除了必须驻留在/Library/LaunchAgents/~/Library/LaunchAgents/ 中的启动代理之外,您还可以将这些文件放在其他任何地方。

最后,我应该提到我用作 Launch Agents 信息的来源: 1234

更新:目前这在 10.8 版中不起作用。此处此处描述基于每个应用程序的变通方法。


Mat*_*hew 5

@flori 提供的答案在 Maverick 上适用于我,前提是我在更改 plist 文件后在终端中运行以下命令

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

killall Finder
Run Code Online (Sandbox Code Playgroud)