如何在Automator中将文件路径传递给AppleScript?

时间:2014-03-25 03:58:22

标签: applescript automator

我希望能够在Finder中右键单击文件或文件夹,然后选择服务>打开终端以在该路径上打开终端窗口。

在automator中,我有一个运行AppleScript的服务

tell application "Terminal"
    do script "cd $filePath"
    activate
end tell

我不知道如何传递文件路径!

奖励:如何确保这适用于文件和文件夹?如果是文件,则可能表示该文件不是目录。

奖金:我自己哪里可以找到这个答案?文档似乎是密集的。

由于

切特

1 个答案:

答案 0 :(得分:9)

请注意顶部的“服务接收选定的...”,这会给出AppleScript的结果。 这将打开文件夹和文件容器,但不会冗余。 enter image description here

on run {input, parameters}
    set didThese to {}
    repeat with f in input
        set f to (f as text)
        if f ends with ":" then
            if f is in didThese then --check to see if we did this already
                --ignore
            else
                tell application "Terminal" to do script "cd " & quoted form of POSIX path of f
                set didThese to (didThese & f) --load into memory for subsequent iterations of loop
            end if
        else
            --first get containing folder, then use that
            tell application "Finder" to set f to ((container of alias f) as alias as text)
            if f is in didThese then
                --ignore
            else
                tell application "Terminal" to do script "cd " & quoted form of POSIX path of f
                set didThese to (didThese & f)
            end if
        end if
    end repeat
    activate application "Terminal"
end run

https://apple.stackexchange.com/questions/112731/automator-service-to-print-a-relative-path-of-selected-files-printing-everything

收集

[编辑:] 顺便提一下,还有一件事要做,就是如何处理捆绑包,例如.app文件,它们不是真正的文件。使用

set i to info for (alias f)

然后

package folder of i

将使脚本能够通过额外的if / then分支来确定这一点。我个人并不介意它“cd”成捆绑。