To make environment variables available to Mac OS X GUI applications, those variables must be defined in your ~/.MacOSX/environment.plist. Sometimes you want to have those same environment variables available from your shell as well. If you're using Terminal, that's no problem--the variables from environment.plist are available. But what if you need to SSH into the machine?
Adhering to the DRY principle, this script can be launched from your shell's rc file to load up the environment variables from environment.plist. To load it into my tcsh environment, I use
if ($?SSH_CLIENT) then
eval `~/bin/parseEnvironmentPlist.rb`
endif
And here's the ruby script:
if /^\/[-A-Za-z\/]+\/(bash|tcsh)$/ =~ ENV['SHELL']
shell = $1
else
exit 1
end
key_re = /^\s*<key>([A-Za-z]+[_A-Za-z0-9]*)<\/key>\s*$/
value_re = /^\s*<string>([-_:.\/0-9A-Za-z]+)<\/string>\s*$/
File.open("#{ENV['HOME']}/.MacOSX/environment.plist", "r") do |plist|
currentKey = ""
plist.each_line do |next_line|
if key_re =~ next_line
currentKey = $1
currentValue = ""
elsif value_re =~ next_line
currentValue = $1
if shell == "bash"
puts "#{currentKey}=#{currentValue}; export #{currentKey};"
elsif shell == "tcsh"
puts "setenv #{currentKey} #{currentValue};"
else
exit 1
end
currentKey = currentValue = ""
end
end
end
I wrote this script back when I was first learning Ruby, so A) that's why there are so many comments and 2) it could probably be improved, but it works for me!