Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Copy variables from command line to GUI environments UNIX
If you use the UNIX command line environment under OS X, you probably know that the environment symbols you set up in your .bashrc file are not reflected in the GUI environment. In the GUI environment, symbol values are read from the file "~/.MacOSX/environment.plist". Apple has a Developer Note which explains this in some detail.

It's tiresome and error prone to attempt to keep these two sets of symbol definitions in sync. The following python script, run from a Terminal window, will generate a new environment.plist file containing ALL of the symbols defined currently in the command line environment. Start a new terminal window and run this script each time you change your .bashrc file. The changes to the environment symbol definitions will become active the next time you log in (the next time enviornment.plist is read).

View the source for environmentPlist.py. Copy and paste into your favorite Unix text editor. Save the script somewhere on your path, and remember to make it executable.

[robg adds: I haven't tested this one, but I did check the script to make sure there's nothing malicious in it.]
    •    
  • Currently 3.50 / 5
  You rated: 5 / 5 (4 votes cast)
 
[12,761 views]  

Copy variables from command line to GUI environments | 11 comments | Create New Account
Click here to return to the 'Copy variables from command line to GUI environments' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Copy variables from command line to GUI environments
Authored by: corvus on Jul 22, '04 03:49:12PM

Looks like the linked source code lost its indentation. It won't work w/o that. Lines 16 and 39 should be indented (4 spaces is good form).



[ Reply to This | # ]
Copy variables from command line to GUI environments
Authored by: merlyn on Jul 22, '04 07:40:30PM

Yeah, Python's brain-damaged like that. I presumed we got rid of "indentation means something" when we got away from Fortran. Who knew?



[ Reply to This | # ]
Copy variables from command line to GUI environments
Authored by: corvus on Jul 23, '04 12:12:45PM

Heh. If you think Python's indentation is that bad, you should really stay away from perl. It's a total mess! <wink>



[ Reply to This | # ]
Copy variables from command line to GUI environments
Authored by: hopthrisC on Jul 23, '04 10:24:05AM

Um... Why?

1. It might not be wise to copy _all_ set variables to the environment.plist. Some of them are set dynacially by the shell, and who knows if they'll be overwritten at login time and what the effect would be...?

2. No need to keep anything in sync by hand, because all the Variables from the environment.plist file are inherited by the terminal and the shell. Just set them in environment.plist and forget about .bashrc!



[ Reply to This | # ]
Copy variables from command line to GUI environments
Authored by: hopthrisC on Jul 23, '04 11:00:18AM
For the daring:

$ cd .MacOSX
$ mv environment.plist ../Library/Preferences/
$ ln -s ../Library/Preferences/environment.plist .

now you can use

$ defaults write environment VARIABLE "content"
$ defaults delete environment VARIABLE "content"

to set the content of VARIABLE to "content".

Note: This will not work if there's anything that writes to your environment.plist during login (or even later), like sshLogin.

[ Reply to This | # ]

Copy variables from command line to GUI environments
Authored by: gdsimms on Jul 23, '04 01:04:36PM

You are correct. After running this script and a logout/login, I could not open a terminal session because of some logic I put in my .bashrc that depends on what is already defined in environment vars.

Also, inexplicably, dragging icons in the finder and desktop was broken.(!) Selecting, context menu, key commands on files worked fine, but dragging just did not happen. What possible cause could there be for this?



[ Reply to This | # ]
Regarding "can't drag"?
Authored by: frodeaux on Jul 26, '04 09:31:03AM

It is reported that any definition of "SECURITYSESSIONID" in
environment.plist causes the "can't drag" behavior you
describe.

If you look at the script of the original article, you'll see that the
variable is explicitly NOT copied into environment.plist. I wonder if its definition somehow got into your environment.plist? Any other "odd" definitions?

---
"Everyone's Entitled To Their Own Crummy Opinion."



[ Reply to This | # ]
Copy variables from GUI to CLI
Authored by: sco08y on Jul 26, '04 11:26:00AM
Another approach would be to modify your startup scripts to read the environment.plist file, going backwards. IMHO, it would be much simpler than constantly running an update script on your XML file! Personally, I don't have so many environment variables that I've needed to do this, but this might work: First, grab Mac::PropertyList:
sudo cpan install Mac::PropertyList
Then add this line to the appropriate rc file: (I added newlines to avoid page widening)
eval `perl -MMac::PropertyList -e 'while(($k, $v) = each 
%{parse_plist(<>)->value()}) { print "$k=$v\n"; }' 
< ~/.MacOSX/environment.plist`
That would work in sh and bash, csh probably needs to change "$k=$v" to "set $k=$v" or something. Also, this doesn't handle odd characters, which you'd want to put before the print statement. The nice thing is that you can edit your environment easily by dropping environment.plist in your Dock.

[ Reply to This | # ]
Copy variables from GUI to CLI
Authored by: sco08y on Jul 26, '04 11:28:04AM

oh, wait, bash and sh need "export $k=$v".

Note that if you want a script to have _temporary_ access to an easily configured .plist file, don't use export.



[ Reply to This | # ]
From a hint submission...
Authored by: robg on Aug 02, '04 09:17:32AM
By 'anonymous' as a new hint, but designed to go here:
Sometimes it's more practical to maintain the environment.plist file as the central piece of information and 'import' it into your .(t)cshrc file, so that the GUI environment gets reflected in the commandline environment.

To do this one can just copy the attached AWK script to ~/.MacOSX/ (or to some other reasonable place) and use it in a .(t)cshrc as follows:
# Parse ~/.MacOSX/environment.plist
eval `awk -f ~/.MacOSX/environment.awk ~/.MacOSX/environment.plist`
-- AWK SCRIPT BEGIN --

# ###################################
# SIMPLE AWK SCRIPT TO PARSE ENVIRONMENT.PLIST
# 2004, Erik Abele. No rights reserved.
# ###################################

/<key>.*<\/key>/ {
s = index($0, "<key>") + 5
e = index($0, "</key>")
key = substr($0, s, e-s)
next
}
/<string>.*<\/string>/ {
s = index($0, "<string>") + 8
e = index($0, "</string>")
string = substr($0, s, e-s)
printf "setenv %s \"%s\";", key, string
next
}

-- AWK SCRIPT END --
Well, it should be pretty simple to adapt this to other shells (bash, zsh, ...).
-rob.

[ Reply to This | # ]
Copy variables from command line to GUI environments
Authored by: Chiwo on Dec 06, '04 07:07:00AM

I just have this in my .profile. The only variable I want exported it P4CONFIG, but you can add others.


# Export my P4CONFIG to the Mac OS X environment so that BBEdit can see it.
# See <http://developer.apple.com/qa/qa2001/qa1067.html>.

mkdir -p $HOME/.MacOSX
cat > $HOME/.MacOSX/environment.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>P4CONFIG</key>
	<string>$P4CONFIG</string>
</dict>
</plist>
EOF


[ Reply to This | # ]