【问题标题】:How Can I Open AppleScript App with Arguments如何使用参数打开 AppleScript 应用程序
【发布时间】:2015-05-29 06:28:52
【问题描述】:

我有一个 AppleScript,它运行扫描到特定文件夹的扫描程序(命令行)。我需要将参数传递给applescript,然后将参数传递给终端。

在终端中,我想运行 open -a /Applications/MyScanApp.app myargument 并运行 AppleScript。我怎样才能通过那个论点?谢谢您的帮助!我通常是一名 PHP 程序员,这对我来说完全不同。

我的 AppleScript:

tell application "Terminal"
    do script "./myscanprogram myargument 2>&1"
end tell

【问题讨论】:

    标签: bash terminal applescript


    【解决方案1】:

    想知道您为什么要使用终端来处理再次使用终端的 AppleScript,但也许我只是不知道您的情况......

    Applescript:

    on run argv
        tell application "Terminal"
            do script "./myscanprogram " & (item 1 of argv) & " 2>&1"
        end tell
    end run
    

    从终端内的 osascript 调用:

    osascript pathToYourScript.scpt myargument
    

    【讨论】:

      【解决方案2】:

      为什么没有人提到quoted form of?当您想将任意数据作为参数发送到应用程序时,您应该使用带引号的形式。当引号、空格和其他特殊字符在给定路径中时,命令将在前面的示例中分解。

      on run argv
          tell application "Terminal"
              do script "./myscanprogram " & quoted form of (item 1 of argv) & " 2>&1"
          end tell
      end run
      

      既然您提到您是 AppleScript 的新手,它必须在 Terminal.app 中运行还是外壳就足够了? AppleScript 有命令 do shell script,它打开一个 shell,执行文本并将标准输出返回给你。

      on run argv
         do shell shell script "/path/to/myscanprogram " & quoted form of (item 1 of argv) & " 2>&1"
      end run
      

      最后但并非最不重要。如果您不希望扫描程序的输出并且不希望 AppleScript 等到它完成,您可以使用

      on run argv
         do script "/path/to/myscanprogram " & quoted form of (item 1 of argv) & " &>/dev/null &"
      end run
      

      【讨论】:

      • 是的,你是对的。我对引用参数两次感到奇怪的恐惧,因为如果输入包含空格,则必须引用它。否则,参数可能会被解析为两个参数而不是一个。但是:我对其进行了测试,最节省的方法是使用 quoted form 确实,如果已经引用了 shell 参数,它也可以工作! ;-) thumbsup
      • 您能否提供一个使用整个 argv 列表概括用例的示例?
      • 我知道我的回复没有回答标题,但确实回答了 SO 的全部内容。您的问题的答案,argv(或on run 之后的任何变量名)始终是“整个 argv 列表”。问题不在于argv 本身,而在于您如何执行脚本并正确传递参数。
      猜你喜欢
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多