bash - 如何使用参数打开 AppleScript 应用程序

标签 bash terminal applescript

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

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

我的 AppleScript:

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

最佳答案

为什么没有人提到 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 中运行或者一个 shell 就足够了? 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

关于bash - 如何使用参数打开 AppleScript 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29258142/

相关文章:

bash - 创建不写入标准输出的用户提示

git - 致命的 : remote server hung up unexpectedly

ios - 第三方库使用的框架

linux - 将标准输入/输出重定向到替代命令

list - 如何将 AppleScript 列表转换为字符串

linux - 庆典错误 : 2: integer expression expected

linux - sed 在多个空格的情况下搜索和替换

terminal - ngrok在后台运行

perl - 如何在 OS X 中编辑文件元数据?

variables - 使用 AppleScript 中的变量播放音乐(歌曲专辑或艺术家)