Get path of parent folder of script location : Applescript

9

3

Background: I'm trying to write a simple applescript app that will launch a tcl app, but I'm getting stuck on the first part of the script.

I need to get the parent folder of the path to the applescript. When I run this code:

set LauncherPath to path to me
set ParentPath to container of LauncherPath

...I get this error:

error "Can’t get container of alias \"Macintosh HD:Users:simon:Downloads:folder with spaces:CrossFire-master:CrossFire Launcher for Mac.app:\"." number -1728 from container of alias "Macintosh HD:Users:simon:Downloads:folder with spaces:CrossFire-master:CrossFire Launcher for Mac.app:"

After reading this answer, I tried this:

set LauncherPath to path to me
set RealLauncherPath to first item of LauncherPath
set ParentPath to container of RealLauncherPath

...but I got this error:

error "Can’t get item 1 of alias \"Macintosh HD:Users:simon:Downloads:folder with spaces:CrossFire-master:CrossFire Launcher for Mac.app:\"." number -1728 from item 1 of alias "Macintosh HD:Users:simon:Downloads:folder with spaces:CrossFire-master:CrossFire Launcher for Mac.app:"

Any help or ideas much appreciated. Thanks in advance!

P.S. once I figure out the above problems, the full script will be something like this:

set LauncherPath to path to me
set RealLauncherPath to first item of LauncherPath
set ParentPath to container of RealLauncherPath
set UnixPath to POSIX path of ParentPath
set launcherCrossFire to "/usr/local/bin/wish " & UnixPath & "/CrossFire.tcl > /dev/null &" -- creat command to launch CrossFire
do shell script launcherCrossFire

UPDATE: Here's the working script incorporating the answer below:

set UnixPath to POSIX path of ((path to me as text) & "::") --get path to parent folder
set LaunchCrossFire to "/usr/local/bin/wish '" & UnixPath & "CrossFire.tcl' > /dev/null 2>&1 &" -- creat command to launch CrossFire
do shell script LaunchCrossFire -- run command

Simon

Posted 2013-11-07T13:50:10.020

Reputation: 193

Answers

17

Try:

set UnixPath to POSIX path of ((path to me as text) & "::")

adayzdone

Posted 2013-11-07T13:50:10.020

Reputation: 592

Thanks, adayzdone! I posted the final code above in the original question. I have one more issue. The launcher app doesn't close after running it. Do you have any ideas for how to work around that? I thought sending the output to dev/null would stop that from happening... – Simon – 2013-11-08T13:06:38.850

Nevermind, I found the answer here. I added "/dev/null 2>&1 &" to the end of the command instead of just "/dev/null &".

– Simon – 2013-11-08T13:34:03.763

1

You should run the script from within a 'Tell Block' like:


tell application "Finder"
get path to me

set a to container of the result
return (a as alias)
-- Or just get the name of the Parent Container like;
set b to name of a
return b
end tell

Capitainos

Posted 2013-11-07T13:50:10.020

Reputation: 11