• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Tell Me to Quit in Unix-ese...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Tell Me to Quit in Unix-ese...


  • Subject: Re: Tell Me to Quit in Unix-ese...
  • From: Christopher Nebel <email@hidden>
  • Date: Mon, 06 Apr 2015 20:42:47 -0700

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

On Apr 6, 2015, at 3:45 PM, email@hidden wrote:

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.
6. apr. 2015 kl. 23:52 skrev Stan Cleveland <email@hidden>:

On Apr 6, 2015, at 2:28 PM, Stan Cleveland <email@hidden> wrote:

On Apr 6, 2015, at 1:46 PM, Oakman <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:
'error number -128'

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:
cancel button "Stop"

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.

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Tell Me to Quit in Unix-ese...
      • From: Oakman <email@hidden>
References: 
 >Tell Me to Quit in Unix-ese... (From: Oakman <email@hidden>)
 >Re: Tell Me to Quit in Unix-ese... (From: Stan Cleveland <email@hidden>)
 >Re: Tell Me to Quit in Unix-ese... (From: Stan Cleveland <email@hidden>)
 >Re: Tell Me to Quit in Unix-ese... (From: email@hidden)

  • Prev by Date: Re: Tell Me to Quit in Unix-ese...
  • Next by Date: Re: Different behavior with shell scripts with AppleScript versus terminal
  • Previous by thread: Re: Tell Me to Quit in Unix-ese...
  • Next by thread: Re: Tell Me to Quit in Unix-ese...
  • Index(es):
    • Date
    • Thread