使用ASObjC亚军中止在AppleScript的shell脚本命令 [英] Using ASObjC Runner to abort a shell script command in Applescript

查看:488
本文介绍了使用ASObjC亚军中止在AppleScript的shell脚本命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 ASObjC亚军 code在我的AppleScript的显示一个进度窗口一次做shell脚本运行。如何让我的进度窗口上的按钮杀死shell脚本?

继承人我的code的例子:

 告诉应用程序ASObjC飞人
    重置进度
    进度窗口集属性{按钮标题:中止,可见按钮:真正的,不确定的:真正}
    启用
    显示进度
告诉结束设置shellOut做shell脚本blahblahblah
显示对话框shellOut告诉应用程序ASObjC飞人隐藏进度
告诉应用程序ASObjC飞人退出


解决方案

有几个部分答案:


  1. 异步做shell脚本正常,做shell脚本只shell命令后返回已完成,这意味着你不能在壳体内的过程行事。但是,你可以得到一个做shell脚本命令通过异步执行<一个href=\"http://mywiki.wooledge.org/ProcessManagement?highlight=%28background%29#How_do_I_run_a_job_in_the_background.3F\"相对=nofollow>一个后台 shell命令它执行,即。

     做shell脚本some_command&安培;&GT; /目标/输出和

    - 这将启动的shell命令后立即返回。因为它不会返回命令的输出,你必须赶上自己,例如在一个文件(或重定向到的/ dev / null的,如果你不需要它) 。如果追加回声$!来的命令,做shell脚本将返回后台进程的PID。基本上,做

     设置thePID做shell脚本some_command&安培;&GT; /目标/输出和!回声$

    看<一个href=\"http://developer.apple.com/library/mac/technotes/tn2065/_index.html#//apple_ref/doc/uid/DTS10003093-CH1-SECTION5\"相对=nofollow>苹果公司的技术说明TN2065 。停止该过程然后做的一个简单的事情做的shell脚本杀&放大器; thePID


  2. 挂钩到的 ASObjC亚军的的进度对话框只是一个投票事项的按钮被pressed 财产和打破真正

     重复,直到(按钮被pressed进度窗口)
        延迟0.5
    重复结束
    如果(按钮是pssed进度窗口的$ P $),然后执行shell脚本杀&放大器; thePID


  3. 当你的shell脚本完成以关闭进度对话框决定:这是最有趣的部分,如shell命令异步操作。最好的办法是掏出来 PS 与PID您检索来检查进程仍在运行,即

     如果(做shell脚本PS -o COMM = -p&放大器; thePID&安培;退出0)是

    将返回真正当进程不再运行。


这让你用下面的code:

 告诉应用程序ASObjC飞人
    重置进度
    进度窗口集属性{按钮标题:中止,可见按钮:真正的,不确定的:真正}
    启用
    显示进度    尝试 - 所以我们可以取消错误对话框显示
        设置thePID做shell脚本blahblahblah&安培;&GT; /文件/描述符放;回声$!
        重复,直到(按钮被pressed进度窗口)
            告诉我,如果(做shell脚本PS -o COMM = -p&放大器; thePID&安培;退出0)是然后退出重复
            延迟0.5 - 高值将使关闭对话框响应少
        重复结束
        如果(按钮是pssed进度窗口的$ P $),然后告诉我做shell脚本杀&放大器; thePID
    年底试    隐藏进度
    放弃
告诉结束

如果您需要捕获你的背景shell命令的输出,你必须把它重定向到文件,完成后读出该文件的内容,如上所述。

I've got ASObjC Runner code in my AppleScript that shows a progress window once a do shell script is run. How do I make the button on the progress window kill the shell script?

Heres a sampling of my code:

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

解决方案

There are several parts to the answer:

  1. Asynchronous do shell script: normally, do shell script only returns after the shell command has completed, which means you cannot act on the processes inside the shell. However, you can get a do shell script command to execute asynchronously by backgrounding the shell command it executes, i.e.

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

    – which will return immediately after launching the shell command. As it will not return the command’s output, you have to catch that yourself, for instance in a file (or redirect to /dev/null if you don’t need it). If you append echo $! to the command, do shell script will return the PID of the background process. Basically, do

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

    see Apple’s Technical Note TN2065. Stopping that process is then a simple matter of doing do shell script "kill " & thePID.

  2. Hooking into ASObjC Runner’s progress dialog is just a matter of polling its button was pressed property and breaking on 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. Deciding when your shell script is done to dismiss the progress dialog: that is the interesting part, as the shell command operates asynchronously. Your best bet is to shell out to ps with the PID you retrieved to check if the process is still running, i.e.

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

    will return true when the process is not running anymore.

Which leaves you with the following code:

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

If you need to capture the output of your background shell command, you will have to redirect it to file and read out that file’s content when done, as noted above.

这篇关于使用ASObjC亚军中止在AppleScript的shell脚本命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆