Append to unix PATH

Hi,

I’m trying to add a folder to the unix PATH. Can someone verify if the following will work.

When I read the “.bash_profile”, I get this:

I’ve read on the internet to add this to the end of .bash_profile:

So, I wrote this script to add the path:

set pp to "/Users/kelhome/.bash_profile"
set pf to POSIX file pp
set t to "export PATH=/Users/kelhome/Python:$PATH"
set ref_num to open for access pf with write permission
write t to ref_num
close access ref_num

I haven’t run this script yet, because I didn’t want to mess things up. I tested the export line in Terminal and it worked, but I wanted it to be permanent.

Edited: one thing I noticed is do I need double quotes around this part:
/Users/kelhome/Python:$PATH

Edited: oops, I forgot jto move the pointer to eof:

set pp to "/Users/kelhome/.bash_profile"
set pf to POSIX file pp
set t to "export PATH=/Users/kelhome/Python:$PATH"
set ref_num to open for access pf with write permission
read ref_num
write t to ref_num
close access ref_num

Thanks,

Model: MBP
AppleScript: 2.2.3
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)

Hello.

The only place I would consider scripting the setting of an environment varialbe, is in an install script, and as such, it is an interesting exercise. Just try to write it out to a text file, and the source the text file with the ‘.’ operator, to see if you got it right. I think you know how to fill in the backslashes, until a string is correctly hyphenated.

do shell script "export avar=\"a var with spaces and \\\"hyphens\\\"\""

To source the file from the command line, use . space or the source keyword in front of a text file.

. mypathexpr

Hi McUsr,

Didn’t know why I didn’t think of the test file. Fixed up the script:

--set pp to "/Users/kelhome/.bash_profile"
set pp to "/Users/kelhome/Desktop/testfile.txt"
set pf to POSIX file pp
set t to "export PATH=\\\"/Users/kelhome/Python:$PATH\\\""
set ref_num to open for access pf with write permission
try
	write t to ref_num starting at (eof)
	close access ref_num
on error err_msg number err_num
	close access ref_num
	error err_msg number err_num
end try

Oh, I see. What you’re saying is that the $PATH might have blanks.

set t to “export PATH=\"/Users/kelhome/Python:$PATH\"”

I need to test this more. It’s a good thing that I didn’t run the original script. I was excited after it worked in Terminal.

Thanks for the hints,

kel

Think I got a working script that works with the test file. I hope it works on the .bash_profile:

--set pp to "/Users/kelhome/.bash_profile"
set pp to "/Users/kelhome/Desktop/testfile.txt"
set pf to POSIX file pp
set t to "export PATH=\"/Users/kelhome/Python:$PATH\""
set ref_num to open for access pf with write permission
try
	write t to ref_num starting at (eof)
	close access ref_num
on error err_msg number err_num
	close access ref_num
	error err_msg number err_num
end try

Used the single \ for the inner quotes.

Thanks a lot,
kel

Had to repost this.

It works in Terminal now even after restart. :slight_smile: But, it doesn’t work in the AppleScript Editor. :confused:

do shell script "HelloWorld.py"

→ error “sh: HelloWorld.py: command not found” number 127

I seem to remember this same thing happening with Jaguar.

See developer.apple.com/library/mac/#technotes/tn2065/_index.html. Especially:

"For security and portability reasons, do shell script ignores the configuration files that an interactive shell would read, so you don’t get the customizations you would have in Terminal. "

Hi Shane,

I remember that article now. When I look at the path it’s different.

do shell script "echo $PATH"

→ “/usr/bin:/bin:/usr/sbin:/sbin”

Trying to make things more easy actually makes things harder.

Edited: with do shell script, I need to export the PATH in the script as in McUsr’s do shell script.

Thanks,