获取脚本位置的父文件夹的路径:Applescript


9

背景:我正在尝试编写一个简单的applescript应用程序,它将启动一个tcl应用程序,但是我陷入了脚本的第一部分。

我需要获取applescript路径的父文件夹。当我运行此代码时:

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

...我得到这个错误:

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:"

阅读此答案后,我尝试了以下方法:

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

...但是我得到了这个错误:

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:"

任何帮助或想法,不胜感激。提前致谢!

PS一旦我解决了以上问题,完整的脚本将如下所示:

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

更新:这是包含以下答案的工作脚本:

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

Answers:


17

尝试:

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

谢谢,adayzdone!我在原始问题中在上面发布了最终代码。我还有一个问题。启动器应用程序运行后不会关闭。您是否有解决此问题的想法?我以为将输出发送到dev / null会阻止这种情况的发生……
Simon

没关系,我在这里找到了答案。我在命令末尾添加了“ / dev / null 2>&1&”,而不仅仅是“ / dev / null&”。
西蒙(Simon)

1

您应该从“告诉块”中运行脚本,例如:


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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.