相对于脚本位置的AppleScript路径

时间:2015-06-22 19:47:39

标签: macos path applescript finder

我尝试启动与我的脚本位于同一目录中的文件夹的Finder窗口。当我运行下面的代码时,它会启动一个空白的Finder窗口,设置为脚本的路径而不是文件夹。

tell application "Finder"
    tell application "Finder" to make new Finder window
    set file_path to (path to me) as text
    set target of Finder window 1 to file_path
end tell

如何获取脚本文件夹的路径,而不是脚本?

2 个答案:

答案 0 :(得分:2)

你很亲密。你不需要文件的文本版本,你只需要文件本身,然后你可以向Finder询问该文件的容器:

tell application "Finder"
    tell application "Finder" to make new Finder window
    set file_path to (path to me)
    set target of Finder window 1 to file_path's container
end tell

答案 1 :(得分:1)

我知道这样做的最短路径是:

tell application "Finder" to open ((path to me as text) & "::")

编辑脚本会产生以下内容:

tell application "Finder"
    make new Finder window -- There is no need for an your second tell statement
    set file_path to (path to me as text) & "::" -- Goes up one directory
    set target of Finder window 1 to file_path
end tell