Using ASObjC Runner to Cancel a Shell Script Command in Applescript

I have an ASObjC Runner code on my AppleScript that shows a progress window after launch do shell script. How to make the button in the execution window kill the shell script?

Here is a sample 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
+1
source share
1 answer

There are several parts to the answer:

  • Asynchronous do shell script: usually do shell scriptreturned only after the shell command completes, which means that you cannot manipulate processes inside the shell. However, you can get a command do shell scriptto asynchronously execute the backgrounding of the shell command that it executes, i.e.

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

    - . , , , ( /dev/null, ). echo $! , do shell script PID . , do

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

    . TN2065. - do shell script "kill " & thePID.

  • ASObjC Runners - 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
    
  • , script , :, , . 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

, , , .

+2

All Articles