1

我的 AppleScript 中有ASObjC Runner代码,一旦do shell script运行 a 就会显示进度窗口。如何使进度窗口上的按钮终止 shell 脚本?

这是我的代码示例:

tell application "ASObjC Runner"
    reset progress
    set properties of progress window to {button title:"Abort", button visible:true, indeterminate:true}
    activate
    show progress
end tell

set shellOut to do shell script "blahblahblah"
display dialog shellOut

tell application "ASObjC Runner" to hide progress
tell application "ASObjC Runner" to quit
4

1 回答 1

2

答案有几个部分:

  1. 异步do shell script通常do shell script只在 shell 命令完成后返回,这意味着你不能对 shell 内的进程进行操作。但是,您可以通过后台do shell script执行它执行的shell命令来获得一个异步执行的命令,即

    do shell script "some_command &> /target/output &"
    

    – 将在启动 shell 命令后立即返回。由于它不会返回命令的输出,因此您必须自己捕获它,例如在文件中(或者/dev/null如果您不需要它,则重定向到)。如果追加echo $!到命令,do shell script将返回后台进程的PID。基本上,做

    set thePID to do shell script "some_command &> /target/output & echo $!"
    

    请参阅Apple 的技术说明 TN2065。停止该过程是一件简单的事情do shell script "kill " & thePID

  2. 挂钩ASObjC Runner的进度对话框只需轮询其button was pressed属性并继续true

    repeat until (button was pressed of progress window)
        delay 0.5
    end repeat
    if (button was pressed of progress window) then do shell script "kill " & thePID
    
  3. 决定何时完成 shell 脚本以关闭进度对话框:这是有趣的部分,因为 shell 命令是异步操作的。您最好的选择是ps使用您检索到的 PID 来检查进程是否仍在运行,即

    if (do shell script "ps -o comm= -p " & thePID & "; exit 0") is ""
    

    true当进程不再运行时将返回。

剩下的代码如下:

tell application "ASObjC Runner"
    reset progress
    set properties of progress window to {button title:"Abort", button visible:true, indeterminate:true}
    activate
    show progress

    try -- so we can cancel the dialog display on error
        set thePID to do shell script "blahblahblah &> /file/descriptor & echo $!"
        repeat until (button was pressed of progress window)
            tell me to if (do shell script "ps -o comm= -p " & thePID & "; exit 0") is "" then exit repeat
            delay 0.5 -- higher values will make dismissing the dialog less responsive
        end repeat
        if (button was pressed of progress window) then tell me to do shell script "kill " & thePID
    end try

    hide progress
    quit
end tell

如果您需要捕获后台 shell 命令的输出,则必须将其重定向到文件并在完成后读出该文件的内容,如上所述。

于 2012-05-10T22:40:03.823 回答