
如何在 Mac OS X 中設定環境變量,以便它們可用於 GUI 應用程式而不使用〜/.MacOSX/environment.plist或者登入掛鉤(因為這些是已棄用)?
答案1
在 Mountain Lion 上所有的/etc/paths
編輯/etc/launchd.conf
都沒有任何效果!
蘋果開發者論壇說:
“更改 .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>
(看:Apple 的 LaunchServicesKeys 文檔)
現在應用程式(在我的例子中為 SourceTree)使用給定的路徑並與 git 1.9.3 一起使用:-)
PS:當然,您必須根據您的特定路徑需求調整路徑條目。
答案2
此解決方案使用 的功能launchctl
,並結合發射代理模仿舊的登入掛鉤。使用 儲存的其他解決方案launchd
,請參閱這個比較。這裡使用的啟動代理位於/庫/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>
一件重要的事情是載入時運行鍵,以便儘早執行啟動代理程式。真正的工作是在 shell 腳本中完成的/用戶/共享/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
注意path_helper
get的調用小路設定正確。最後,〜/.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
這些是launchctl
命令,請參閱其線上說明頁面以獲取更多資訊。對我來說效果很好(我應該提到我仍然是雪豹人),GUI 應用程序,例如紡織工作室和電子商店可以看到我自己的tex樹。可以改進的地方:
shell腳本
#filename="$1"
中有一個。這並非偶然,因為檔案名稱應該由啟動代理程式作為參數提供給腳本,但這不起作用。如上所述這裡(德語且需要付費!),可以將腳本放入啟動代理本身。
我不確定這個解決方案有多安全,因為它
eval
與用戶提供的字串一起使用。我想記住,使用這種方法定義 MANPATH 效果不佳,但我不確定。
應該要提到的是,蘋果公司打算採取類似的方法,將東西放入∼/launchd.conf,但目前不支援該日期和作業系統(請參閱 的線上說明頁launchd.conf
)。我想像通配符這樣的事情不會像本提案那樣運作。當然,人們可以將這些文件放在其他任何地方,除了啟動代理,它必須駐留在/庫/LaunchAgents/或者〜/庫/LaunchAgents/。
答案3
@flori 提供的答案在 Maverick 上適用於我,前提是我在更改 plist 檔案後在終端機中執行以下命令
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user
killall Finder
答案4
另一種選擇是使用/etc/launchd.conf
.例如,我PATH
透過添加以下行來更改預設值/etc/launchd.conf
:
setenv PATH ~/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/libexec:/usr/texbin
/etc/launchd.conf
您可以透過重新啟動或執行launchctl < /etc/launchd.conf; sudo launchctl < /etc/launchd.conf
、終止並重新啟動進程來套用變更。
設定/etc/launchd.conf
適用於根 launchd 進程和每個使用者 launchd 進程。用setenv
in設定的環境變數由和/etc/launchd.conf
兩者顯示。sudo launchctl export
launchctl export