bash/sh, unlike AppleScript, doesn’t particularly care that a command in the middle of a script failed — by default, it will keep going, which is why your initial attempt didn’t work. That also means that changing the AppleScript part of the script won’t help — you need to change the bash part to pay attention to the result of osascript(1). Tommy’s suggestion of checking the exit status is the simplest way to handle it, except that there’s a more convenient construct: “&&” and “||”. Using one of them between two commands means “do the second command only if the first one succeeded (or failed, respectively).”
Stan’s suggestion to expand the script by catching the error and returning distinguishable output is useful, but unnecessary in this case — because “display dialog” throws an error when cancelled, and AppleScript errors turn into a non-zero return status, just checking the exit status is good enough. Put all that together, and you get something like this:
echo "stuff" osascript -e 'display dialog "Keep going?"' || exit 0 echo "more stuff"
It will only print both lines if you press the “OK” button.
—Chris Nebel AppleScript Engineering
You could also test for the exit code of the previous process that has terminated.The code below, should be put below your osascript. if your osascript do exit with any error number then the variable $? should have the value 1, at least that is what my experiments show me. ------- if [ $? -ne 0 ] ; then exit 1 fi ----- You can of course play with this, by issuing echo $0 after any osascript command. On Apr 6, 2015, at 2:28 PM, Stan Cleveland < email@hidden> wrote: I want to give the user the opportunity to cancel out of the script, however I know not how to achieve that.
Hi Oakman,
in place of this: '<kill script>' Put this: You can safely eliminate the following unnecessary code, as well: cancel button "Cancel" The "Cancel" button is by definition the default cancel button. You'd specify this only if using a different button name, like this:
Hi again,
My bad! Forget what I posted (above) without actually testing it.
Use this instead (all in one line, not three, of course):
osascript -e 'tell application id \"com.apple.systemevents\"' -e 'try' -e 'display dialog \"Do you want to continue?\" buttons {\"Cancel\", \"OK\"}' -e 'on error number -128' -e 'return \"aborted\"' -e 'end try' -e 'return \"continued\"' -e 'end tell'
This will not only let the user exit, but allows feedback to your script, if desired.
|