1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Just exactly what I was looking for. I had an idea of what to do but needed some direction to get it going. Thanks so much for the detailed response. You ROCK!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.