Add your path into PATH for GUI Application for MacOS


MacOS is built based on FreeBSD, so you can set the variable PATH for those application which can be launched from command line. But if you only add the application bin path in .bashrc or other login script, there is no impact to MacOS GUI application.

Today, when I was installing the Git MacOS version, I found a script to modify the ./MacOSX/environment.plist to append the bin path for GUI environment.


#!/bin/sh

append_path () {
input="$1"
value="$2"
if ! echo $input | /usr/bin/egrep -q "(^|:)$value($|:)" ; then
if [ "$3" = "after" ] ; then
echo $input:$value
else
echo $value:$input
fi
else
echo $input
fi
}

append_plist_var() {
name="$1"
append_value="$2"
default_value="$3"
current_value="`defaults read $HOME/.MacOSX/environment ${name}`"
[ ! "$current_value" ] && current_value="$default_value"
new_value="`append_path "$current_value" "$append_value" after`"
defaults write $HOME/.MacOSX/environment "$name" "$new_value"
if [ "$current_value" == "$new_value" ]; then
echo "No change to $name in ~/.MacOSX/environment.plist"
else
echo "Variable $name in ~/.MacOSX/environment.plist changed from '$current_value' to '$new_value'"
echo "You will need to log out of your Mac OS X user account and log back in for changes to take effect."
fi
}

append_plist_var PATH "/usr/local/git/bin" "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/local/bin"

pushd "$HOME"

popd

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s