【问题标题】:How to pass file path to AppleScript in Automator?如何在 Automator 中将文件路径传递给 AppleScript?
【发布时间】:2014-03-25 15:58:25
【问题描述】:

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

在自动机中,我有一个运行 AppleScript 的服务

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

不知道怎么传文件路径!

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

奖励:我自己在哪里可以找到这个答案?文档似乎很密集。

谢谢

切特

【问题讨论】:

  • (在 OSX 10.6.8 上测试过——旧的,我知道。如果您的版本不喜欢,请告诉我)

标签: applescript automator


【解决方案1】:

请注意顶部的“服务接收选定的 ...”,它将结果提供给 applescript。 这将打开文件夹和文件容器,但不会是多余的。

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”成捆绑包。

【讨论】:

    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    相关资源
    最近更新 更多