【问题标题】:Automator - dealing with spaces on paths that will be passed from Applescript to Shell ScriptAutomator - 处理从 Applescript 传递到 Shell Script 的路径上的空格
【发布时间】:2019-11-24 11:56:56
【问题描述】:

我有一个 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 脚本时,它不会接受。

【问题讨论】:

    标签: bash applescript automator


    【解决方案1】:

    在与您的其他 question 非常相似的情况下,您似乎正在创建一个不需要解决的问题,方法是选择使用 AppleScript 操作来接收最终传递给 shell 脚本操作的文件路径.这样做的唯一原因似乎是让 AppleScript 将文件路径“处理”为alias 文件引用,将它们转换为 posix 路径,以便将它们用作 shell 脚本中的命令行参数。

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

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

    结论

    ▸ 删除您的 AppleScript 操作,只需将文件直接发送到 shell 脚本,as arguments,并将您的命令行参数变量用引号括起来,正如 @vadian 已经演示过的那样。

    ▸ 如果您出于其他原因使用 AppleScript,那很好。只需将 alias 引用原样保留,然后将它们发送到 shell 脚本,并按照刚才描述的方式处理它们。


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

    【讨论】:

    • 绝对正确; alias 引用可以成功地传递到 Automator 中的 shell 脚本操作 - 无需事先转换为 POSIX。仅当路径表示为静态 file 对象引用时,才需要在传递到 shell 脚本操作之前进行路径转换 - 在这种情况下,需要事先转换为 alias
    【解决方案2】:

    只需引用shell行中的路径并删除搜索和替换部分

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

    【讨论】:

    • 简直太棒了!谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!跨度>
    【解决方案3】:

    您可以引用整个字符串或使用quoted form of 智能地引用字符串,您也可以按照您的要求转义单个字符,但在这种情况下您需要转义每个转义字符,例如:

    quoted form of “/Users/myUser/Desktop/my video file.png”
    “/Users/myUser/Desktop/my\\ video\\ file.png”
    

    【讨论】:

    • 不能保证quoted form of 用反斜杠转义空格。通常它将路径用单引号括起来。但是,在将参数从 AppleScript 传递到 ShellScript 动作时,单引号不起作用。
    • quoted form of 不需要转义空格,因为整个字符串根据需要用双(或单)引号引起来,但是用转义字符引用字符串基本上会忽略它们,因为它们会被按字面意思对待。
    猜你喜欢
    • 2020-08-09
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 2011-06-01
    • 2020-04-30
    • 1970-01-01
    相关资源
    最近更新 更多