是否可以在对 `do shell script` 的 applescript 调用中使用命令替换?

Is it possible to use command substitution in an applescript call to `do shell script`?

我有一个自动化服务,我正在更新它以组合处理一系列输入文件(而不是像目前那样连续处理)。它做了很多事情,但在它的一个组件中,我需要处理 N 个文件的内容,并将每个文件的输出处理交给一个 paste 命令,以将其组合起来并进一步处理组合。在命令行上,我会用进程替换来完成,例如:

paste <(commands processing file 1) <(commands processing file 2) ... | other processing commands

但是如果我从 applescript 中这样做,就像这样:

set output to (do shell script "paste <(commands processing file 1) <(commands processing file 2) ... | other processing commands")

我从 applescript 收到一个错误:

The action “Run AppleScript” encountered an error: “Finder got an error: sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `paste <(commands processing file 1) <(commands processing file 2) ... | other processing commands'”

我了解到这是因为 sh 没有做 bash 做的花哨的事情,例如进程替换。

我知道我可以只编写临时文件来实现我的目标,但如果有解决办法,我宁愿不必这样做。

我尝试使用 bash -s:

来解决这个问题
set output to (do shell script "bash -s <<'EOF'" & return & "paste <(commands processing file 1) <(commands processing file 2) | other processing commands" & return & "EOF")

但这会产生同样的错误。

知道如何在不必编写临时文件的情况下完成此操作吗?

UPDATE:我意识到我把问题简化了太多。还有更多。我正在使用的一系列命令(包括已经提到的 paste 命令)包括包含变量和单引号的单行代码,因此解决方案必须防止 shell 插值变量和不干扰单引号。我将更新下面的玩具示例以包含这些详细信息。


玩具示例 (applescript):

文件 1:

this is the first test

文件 2:

this is the second test

苹果脚本:

set file1 to "~/file1.txt"
set file2 to "~/file2.txt"
set output to (do shell script "paste <(head " & file1 & " | perl -ne 'chomp;print(substr($_,0,4),qq(\n))') <(head " & file2 & " | perl -ne 'chomp;print(substr($_,-4),qq(\n))')"
display dialog output

预期输出:

this    test

注意,还有其他变量和另一个使用单引号的命令 (awk)。

天哪!在我发布这个之后,我意识到我需要做的就是逃避 parens。 bash -s 我走在了正确的轨道上!我认为写出问题只是我必须经历的过程才​​能找到答案!

set output to (do shell script "bash -s <<'EOF'" & return & "paste \<\(commands processing file 1\) \<\(commands processing file 2\) | other processing commands" & return & "EOF")

真不敢相信我在发布问题之前没有看到它!

以下对我有用:

set file1 to "~/file1.txt"
set file2 to "~/file2.txt"
set output to do shell script "bash -c \"paste <(head " & file1 & ") <(head " & file2 & ")\""
display dialog output