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:
Asynchronous
do shell script: normally,do shell scriptonly returns after the shell command has completed, which means you cannot act on the processes inside the shell. However, you can get ado shell scriptcommand to execute asynchronously by backgrounding the shell command it executes, i.e.– 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/nullif you don’t need it). If you appendecho $!to the command,do shell scriptwill return the PID of the background process. Basically, dosee Apple’s Technical Note TN2065. Stopping that process is then a simple matter of doing
do shell script "kill " & thePID.Hooking into ASObjC Runner’s progress dialog is just a matter of polling its
button was pressedproperty and breaking ontrue: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
pswith the PID you retrieved to check if the process is still running, i.e.will return
truewhen the process is not running anymore.Which leaves you with the following code:
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.