Saturday, May 25, 2019

#1 2006-04-04 12:25:29 pm

dionysis
Member
Registered: 2006-04-04
Posts: 4

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?


Filed under: terminal

Offline

 

#2 2006-04-04 02:00:23 pm

tugboat666
Member
From:: Marysville, OH
Registered: 2003-01-21
Posts: 92

Re: how to get the directory a script resides in

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)

Offline

 

#3 2006-04-04 02:41:30 pm

dionysis
Member
Registered: 2006-04-04
Posts: 4

Re: how to get the directory a script resides in

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


Filed under: terminal

Offline

 

#4 2006-04-04 03:02:58 pm

waltr
Member
Registered: 2005-09-13
Posts: 317

Re: how to get the directory a script resides in

hi d,

Applescript:


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?


I use Google or any search engine to find out things I don't know!


Filed under: terminal

Offline

 

#5 2006-04-04 03:38:45 pm

dionysis
Member
Registered: 2006-04-04
Posts: 4

Re: how to get the directory a script resides in

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?

Offline

 

#6 2006-04-04 04:03:46 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4665

Re: how to get the directory a script resides in

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

Applescript:

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


iMac running OS X 10.13.1

Offline

 

#7 2006-04-04 04:34:58 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4665

Re: how to get the directory a script resides in

Another way uses the Finder's "container" term (but the other one is faster):

Applescript:

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.


iMac running OS X 10.13.1


Filed under: Finder

Offline

 

#8 2006-04-05 04:59:04 am

kai
Member
From:: Brighton, UK
Registered: 2005-05-28
Posts: 912

Re: how to get the directory a script resides in

Adam Bell wrote:

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


This works better for me, Adam:

Applescript:

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


kai

Offline

 

#9 2006-04-05 05:47:05 am

dionysis
Member
Registered: 2006-04-04
Posts: 4

Re: how to get the directory a script resides in

I bnow came up with this solution:

Applescript:


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

Applescript:

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


Filed under: Finder

Offline

 

#10 2006-04-05 06:05:21 am

kai
Member
From:: Brighton, UK
Registered: 2005-05-28
Posts: 912

Re: how to get the directory a script resides in

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

Applescript:

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


kai


Filed under: Finder

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)