how to get the directory a script resides in

I am supposed to write a script that does the following

  • check what directory the script has been started from (e.g. a USP-Stick)
  • set a system variable (PSIDATADIR) to that directory name
  • run a program that resides in the same directory as the script (the program reads the system variable, and checks this directory for a settings file)

I know i can change a system variable by doing the following.

set psidirectory to “PSIDATADIR="THE_FOLDER_OF_THE_SCRIPT"”
tell application “Terminal”
do script psidirectory
end tell

but this seems to work only temporary, so when I close the terminal the system varaibles are reset.

My specific problems are:

  • how do I get the directory that the script started from?
  • how do I run the program in this specific folder (not an older version that is somewhere else on the computer)
  • how do I change the system variable to stay like this for a while (e.g. until the program shuts down)

any ideas?

  1. set path_ to (path to me as string)

Will give the path to your script application.

Not so sure about the rest. A shell expert will chime in with that…

Andy

Browser: Safari 412
Operating System: Mac OS X (10.4)

Thanks.

So the script looks like this now:

set path_ to (path to me as string)
set slash_ to (ASCII character 34)

set change_system_variable_command to “PSIDATADIR=” & slash_ & path_ & slash_

tell application “Terminal”
do script change_system_variable_command
end tell

the variable is set, but in a kind wof way I cannot use:

Macintosh HD:Applications:psi:testfolder

i cannot do a “cd” to the folder because it has a space between “Macintosh” and “HD” I guess is there a way to change that?

thanks in advance
Robin

hi d,


set path_ to (path to me as string)
set thePath to path_ as alias
set slash_ to (ASCII character 34)

set change_system_variable_command to "PSIDATADIR=" & slash_ & POSIX path of thePath & slash_

tell application "Terminal"
    do script change_system_variable_command
end tell

how’s that?

Wow! You guys are quick. Thanks a lot.

The script somehow works.
One problem left. “path to me” seems to return not only the folder the script is located in but as well the filename of the script (in my case it returns /Volumes/psi_2/psi-script.app with psi-skript.app as the scripts name). How do I remove the filename from this variable and just get the folder?

This little script works for me when I save it as an application (otherwise you just get the path to the Script Editor):

set myPath to POSIX path of (path to me as string) -- gets it in "/" form
set ASTID to AppleScript's text item delimiters -- save 'em
set AppleScript's text item delimiters to "/"
set theFolder to (text 1 thru text item -2 of myPath) & "/" as Unicode text
set AppleScript's text item delimiters to ASTID -- restore 'em
display dialog theFolder -- or whatever

Another way uses the Finder’s “container” term (but the other one is faster):

set myPath to path to me
tell application "Finder"
	set myFolder to (container of myPath) as string
end tell
-- returns the path to the folder containing your script. 
-- You can then append
-- another file name, or use it as an alias.

This works better for me, Adam:

set d to text item delimiters
set text item delimiters to "/"
set f to (POSIX path of (path to me))'s text 1 thru text item -3 & "/"
set text item delimiters to d
display dialog f

I bnow came up with this solution:


set path_ to (path to me)
tell application "Finder"
	set theFolder to (container of item path_)
end tell

set thePOSIXFolder to ((POSIX path of (theFolder as alias)) as string)

the System variable is set by using

do schell script "export VARIABLENAME=\"value\""

Incidentally, since POSIX path returns Unicode text, coercions to text or to Unicode text shouldn’t normally be necessary. For example:

tell application "Finder" to set f to POSIX path of ((container of (path to me)) as alias)