在 Finder 中使用突出显示的文件作为 Bash 变量(Applescript,macOS 10.13)

Use highlighted file in Finder as Bash variable (Applescript, macOS 10.13)

我想设置一个小程序来以非标准格式(例如非 .zip/.cpgz/.tar)归档我的文件,只使用 Applescript 和每个的命令行版本格式的相应工具。我需要弄清楚的最后一件事是如何通过 Applescript 获取 Finder 中当前突出显示的文件的路径,以便我可以将其通过管道传输到 bash。

我搜索了一下,似乎无法通过搜索引擎或此论坛找到此问题的答案。我已经发现并且仍然发现 Apple 的文档有大约 40/60 的可能性过于稀疏 and/or 过时,或者对于任何给定主题甚至根本不存在,所以我选择尽可能不使用它。请不要只是 link Apple 文档页面,真实世界的示例总是值得赞赏的。

target 平台是 macOS 10.13.2、Automator 2.8、Applescript 2.7。

有不止一种方法可以做到这一点,而且还取决于如何选择 Finder items。这是一个例子,如果在 Finder 中只选择了一个 item:

set theTarget to POSIX path of (application "Finder"'s selection as string)

或者,如果路径名有空格,使用:

set theTarget to quoted form of POSIX path of (application "Finder"'s selection as string)

没有显示的示例,其他方法可能取决于 how/where 它属于您的 代码 的其余部分以及是否超过一个 item 在 Finder 中被选中,但这是一个开始的地方。


顺便说一句,如果您从 bash 执行 AppleScript,而不是 Script Editor,其他示例是 运行,然后在bash中使用osascript到运行相同的exampleAppleScript code 如上所示,在 Finder 中选择了一个 item,它将是:

theTarget="$(osascript -e "POSIX path of (application \"Finder\"'s selection as string)")"

或:

theTarget="$(osascript -e "quoted form of POSIX path of (application \"Finder\"'s selection as string)")"