PATH variable for gui programs (Mac Os)

Sometimes you need to edit PATH variable.
Of course you edit .bash_profile, .bashrc, .profile or other scripts and this works fine only in terminal.
GUI apps at Mac Os use its own PATH variable (moreover has its own environment), which can be set using ‘launchctl setenv PATH <PATH_VALUE>’.
So you need to setup PATH for GUI apps correctly, and do not forget about PATH var for terminal.

Moreover, after update to Mac Os X 10.10 Yosemite i was wondering about this:

launchctl getenv PATH

# empty response

Reasons for such behaviour is unknown for me now (even after readind dozen of man pages and all latest recipes connected to this issue at stackoverflow). So if you add something to PATH:

OLD=`launchctl getenv PATH`
launchctl setenv PATH

then you cant access from gui programs some applications at system directories (/bin:/sbin etc).
I just decided to init empty PATH var by contents of /etc/paths.

The main question: what is the way to set up GUI PATH variable using script, reuse PATH variable from terminal settings, and start this script automatically. Daemon and agents mechanism gives an answer.

This script to setup PATH var:

# patch PATH for gui (and spotlight in particular)

# save old value of PATH var
OLD_GUI_PATH=`launchctl getenv PATH`

# if initial path is empty fill it using /etc/paths, otherwise
# you cant use apps in system directories
if [[ -z "$OLD_GUI_PATH" ]]
    OLD_GUI_PATH=`cat /etc/paths | tr "\n" ":" | sed 's/:$//'`

# load .profile (i save terminal PATH modification here)
# so i want to reuse this settings for gui applications
# all my additional paths are saved as ADDITIONAL_PATH variable
# one of my .profile file looks like
# export ADDITIONAL_PATH=...
source ~/.profile

# patch gui PATH varible
    launchctl setenv PATH $NEW_GUI_PATH

Than create plist file to inform launchd daemon to start your script:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
    <!-- to avoid absolute path

where PATH_TO_SCRIPT - absolute path to script descrived above, RELATIVE_PATH - relative path inside user directory. Place this file into ~/Library/LaunchAgents/.

Using this all your gui apps will use terminal PATH variable (no need to set PATH twice for terminal and gui apps) and the problem with gui PATH also will be solved.