【问题标题】:Set two items as input to a shell script inside automator将两项设置为 automator 内的 shell 脚本的输入
【发布时间】:2017-08-31 11:17:01
【问题描述】:

我有这个作为图像查找服务添加的 Automator。

这一切都始于用户选择一堆图像并在服务菜单上选择脚本。

然后脚本首先问用户两个问题:

on run {input, parameters}
    choose from list {"iPhone", "iPad", "Mac"} with prompt "Choose Platform"
    set platform to the result as string

    display dialog "Choose File Name" default answer ""
    set fileName to the result as string

    // here it goes the magic line

    return input
end run

当场“这就是神奇的线”我想添加一条线,将platformfileName 添加为将传递给shell 的参数(输入?)数组(?)上的两个条目将遵循此 AppleScript 的脚本。

shell 脚本将包含以下几行:

platform=$1
shift

fileName=$1
shift

for f in "$@"; do

    // files will be processed here

我不确定 shell 脚本将如何接收它,但据我所知,它应该接收类似数组的东西,由 3 项组成:平台、文件名和所选文件列表。我了解 shift 将删除输入开头的项目。

出于这个原因,我尝试了以下几行:

set beginning of input to platform as string and fileName as string

还有

set beginning of input to platform
set beginning of input to fileName 
//hoping it will push platform to the second position

但没有任何效果。

任何想法

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

    这应该会为您提供魔法:

    on run {input, parameters}
        set platform to (choose from list {"iPhone", "iPad", "Mac"} with prompt "Choose Platform") as string
        set fileName to text returned of (display dialog "Choose File Name" default answer "") as string
        set tempList to {}
        set end of tempList to platform
        set end of tempList to fileName
        repeat with i from 1 to count of input
            set end of tempList to item i of input
        end repeat
        copy tempList to input
        return input
    end run
    

    这也有效,我刚刚测试过:

    on run {input, parameters}
        set platform to (choose from list {"iPhone", "iPad", "Mac"} with prompt "Choose Platform") as string
        set fileName to text returned of (display dialog "Choose File Name" default answer "") as string
        set beginning of input to fileName
        set beginning of input to platform
        return input
    end run
    

    请注意,这只是示例代码,并没有采用任何错误处理。

    我相信在这个用例中,您需要一次将项目添加到列表中。

    【讨论】:

    • @SpaceDog,既然你说过你也试过set beginning of input to platform,我没有费心去测试,只是采取了相反的过程。尽管如此,我刚刚测试了set beginning of input to fileName,然后在下一行测试了set beginning of input to platform,它对我有用。结果是{"iPhone", "hello world", alias "Macintosh HD:Temp:image file 2.png", alias "Macintosh HD:Temp:imagefile1.png"}。然后 Run Shell Script 按预期使用它。不知道为什么它对你不起作用。
    猜你喜欢
    • 2015-06-26
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多