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

T V*_*cor 8 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
Run Code Online (Sandbox Code Playgroud)

dj *_*zie 10

为什么没有人提到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
Run Code Online (Sandbox Code Playgroud)

既然你提到你是AppleScript的新手,它必须在Terminal.app中运行还是足够的shell?AppleScript具有do shell script打开shell,执行文本并将stdout返回给您的命令.

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

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

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


Sho*_*rKo 5

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

苹果文字:

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

终端内的osascript调用:

osascript pathToYourScript.scpt myargument
Run Code Online (Sandbox Code Playgroud)