【问题标题】:How to pass a variable from shell script to applescript如何将变量从shell脚本传递给applescript
【发布时间】:2012-10-22 21:41:12
【问题描述】:

我有一个 shell 脚本,它检查文件夹是否有一定数量的文件并向用户显示错误消息。我使用 applescript 来显示错误消息。有没有办法将shell脚本的变量传递给AppleScript?

这是我所拥有的:

declare -i count=5;
osascript -e 'tell app "Xcode" to display dialog "Counted $count items." buttons {"Continue"}'

我希望输出为Counted 5 items.,但输出为Counted $count items. 如何将shell 脚本变量传递给applescript?

【问题讨论】:

    标签: shell applescript


    【解决方案1】:

    有三种解决方案:

    1. 使用 "" 代替 '' 以便 shell 扩展 $var

    2. 关闭引号,放置 var,然后重新打开,不带空格(例如,'string'$var'rest of string')。但是,如果变量可以包含空格,则这是不安全的。

    3. Use formal command line arguments 与您的 AppleScript 脚本

    【讨论】:

    • 干杯。我使用了第二种方法,因为我没有空格,而且它最容易实现。
    • 如果 var 包含空格,我发现这可以工作:osascript -e 'display dialog "string1'"$var"'string2"'
    【解决方案2】:

    澄清一下,这就是我最终为使其正常工作所做的工作。

    declare -i count=5;
    osascript -e 'tell app "Xcode" to display dialog "Counted '$count' items." buttons {"Continue"}'
    

    【讨论】: