在 Automator 服务中获取多个文件/文件夹的 Unix 路径

在 Automator 服务中获取多个文件/文件夹的 Unix 路径

我想通过在 Finder 中选择 Automator 服务中的(多个)文件、文件夹来获取它们的路径,然后在 shell 命令中使用它们。

我已经有类似的东西了:

启动 AppleScript:

on run {input, parameters}
tell application "Terminal"
    activate
    do script with command "clamscan --bell -i " & POSIX path of input
end tell

结束运行

这有效,但仅适用于一个文件或文件夹,并且不会转换/path/file with spaces/path/file\ with\ spaces

那么,该如何解决呢?

答案1

由于您是在自动机,与运行 AppleScript 采取行动,这将满足您的需要:

on run {input, parameters}

    set theItemsToScanList to {}
    repeat with i from 1 to count input
        set end of theItemsToScanList to quoted form of (POSIX path of (item i of input as string)) & space
    end repeat

    tell application "Terminal"
        activate
        do script with command "clamscan --bell -i " & theItemsToScanList
    end tell

end run

没有必要把事情复杂化并经历其他答案中所示的繁琐程序!


或者如果你选择在平原上做苹果脚本 脚本/应用程序,那么这将满足您的需要:

set theseItems to application "Finder"'s selection

set theItemsToScanList to {}
repeat with i from 1 to count theseItems
    set end of theItemsToScanList to quoted form of (POSIX path of (item i of theseItems as string)) & space
end repeat

tell application "Terminal"
    activate
    do script with command "clamscan --bell -i " & theItemsToScanList
end tell

笔记:例子 苹果脚本 代码以上只是这些,并不包括任何错误处理视情况而定/需要/想要,用户有责任添加任何错误处理对于任意示例代码提出和或代码自己写的。

答案2

我使用最新版本的 Sierra,效果很好

property posixPathofSelectedFinderItems : {}
property QposixPathofSelectedFinderItems : {}
-- stores the selected files in the active finder window
-- in the variable "these_items"
tell application "Finder"
    set these_items to the selection
end tell

-- returns the Posix Path of each of those files and 
-- stores all of that info in the "posixPathofSelectedFinderItems"
-- as a list
repeat with i from 1 to the count of these_items
    set this_item to (item i of these_items) as alias
    set this_info to POSIX path of this_item
    set end of posixPathofSelectedFinderItems to this_info
end repeat

repeat with i from 1 to number of items in posixPathofSelectedFinderItems
    set this_item to item i of posixPathofSelectedFinderItems
    set this_item to quoted form of this_item
    set end of QposixPathofSelectedFinderItems to (this_item & " ")
end repeat
tell application "Terminal"
    activate
    do script with command "clamscan --bell -i " & items of QposixPathofSelectedFinderItems
end tell
set posixPathofSelectedFinderItems to {}
set QposixPathofSelectedFinderItems to {}

相关内容