Automator-处理将从Applescript传递到Shell脚本的路径上的空格

时间:2019-07-15 18:45:29

标签: bash applescript automator

我有一个AppleScript处理别名形式的路径并将其转换为posix。

这些路径将在此自动化工作流程的第二步中由Shell脚本处理。

问题在于路径包含空格。

我不知道该如何处理,因此我有此子例程来删除任何空间并用\替换。

on replaceChars(this_text, search_string, replacement_string)
    set saveTID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to saveTID
    return this_text
end replaceChars

我在Applescript部分

-- convert the path from the alias form to posix
set fileInputPosix to (the POSIX path of thePath)

-- replace " " with "\ "
set quotedForm to replaceChars(fileInputPosix, space, "\\ ")

这时路径似乎正常,类似

'/Users/myUser/Desktop/my\ video\ file.png'

但是当我将此路径传递给shell脚本时,它将不会接受。

3 个答案:

答案 0 :(得分:2)

只需引用外壳线中的路径并删除搜索并替换零件

/usr/local/bin/ffmpeg -I "$1" -vf "select=eq(n\,$3)" -vframes 1 "$2"

答案 1 :(得分:2)

在与您的其他question类似的情况下,您似乎通过选择使用AppleScript动作来接收最终最终传递到Shell中的文件路径而产生了不需要解决的问题脚本动作。看来这样做的唯一原因是让AppleScript将文件路径作为alias文件引用进行“处理”,并将其转换为posix路径,以便将它们用作shell脚本中的命令行参数。 / p>

这完全没有必要。即使您出于其他原因决定必须使用AppleScript, Automator 在动作之间传递不兼容的数据类型时,通常也会将不兼容的数据类型转换为适当的格式,因此AppleScript {{1 }}引用可以很高兴地发送到shell脚本动作中,它将自动将它们作为posix文件路径接收:

How file paths are processed in an Automator workflow

在上面的屏幕截图中,您可以看到我在任何阶段均未执行任何处理或操作,并且两个脚本操作均返回了该语言所期望的正确文件路径。

结论

▸删除AppleScript操作,只需将文件直接发送到Shell脚本 alias ,然后将命令行参数变量括在引号中@vadian已经证明了。

▸如果您出于其他原因使用AppleScript,那就很好。只需完全保留as arguments引用,然后将其发送到Shell脚本,并按照上述方法对其进行处理即可。


从另一个方向发送的文件路径(从Shell脚本到AppleScript)不会神奇地成为alias引用。 posix路径只不过是一段文本,而且由于AppleScript能够很好地处理文本,因此没有期望尝试将其强制转换为其他文本。如有必要,这需要脚本编写者明确地完成。

答案 2 :(得分:1)

您可以引用整个字符串,也可以使用quoted form of巧妙地引用字符串,并且您也可以在尝试操作时转义单个字符,但是在这种情况下,您需要转义每个转义字符,例如例如:

quoted form of “/Users/myUser/Desktop/my video file.png”
“/Users/myUser/Desktop/my\\ video\\ file.png”
相关问题