How to Use Cancel Button in Osascript to Stop Running Script Here is my basic script:

#!/bin/bash

osascript  -e 'tell application id "com.apple.systemevents"'
-e 'display dialog "Do you want to continue?" &amp; return &amp; return &amp;
 " Please wait..." buttons {"Cancel", "Okay"} default button
2 cancel button "Cancel"' -e 'end tell' -e 'if button returned is "Cancel" then'
-e '&lt;blah blah kill this script&gt;' -e 'end if'

-- other bash stuff here


I need the script to stop if the user clicks the "Cancel" button. As it is now the script waits until the user clicks either button and then proceeds to run the bash code. How can I kill the script?

This to be able to run on Mac OS 10.6 through 10.10, no third-party add-ons.
How about this? 


It works on my 10.6 Macbook. 
It is cleaner than having to escape quotes etc...
osascript returns a status like normal unix programs.
Bash script aborts on a status 1 from osascript.
No unnecessary messages from osascript - &gt;dev/null 2&gt;&amp;1 


I don't have anything else to test it on. You may need to adjust for other versions of OSX.

#!/bin/bash

osascript &gt;/dev/null 2&gt;&amp;1 &lt;&lt;-EOF
tell application id "com.apple.systemevents"
   set myMsg to "Do you want to continue?" &amp; return &amp; return &amp; " Please wait..."
   set theResp to display dialog myMsg buttons {"Cancel", "Okay"} default button 2 
end tell

# Following is not really necessary. Cancel returns 1 and OK 0 ...
if button returned of theResp is "Cancel" then
   return 1
end if
EOF

# Check status of osascript
if [ "$?" != "0" ] ; then
   echo "User aborted. Exiting..."
   exit 1
fi

#-- other bash stuff here
echo "All good, moving on...."


HTH