• 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: A 'Stop' handler for when script is run in editor?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A 'Stop' handler for when script is run in editor?


  • Subject: Re: A 'Stop' handler for when script is run in editor?
  • From: kai <email@hidden>
  • Date: Thu, 17 Feb 2005 04:48:05 +0000


On Wed, 16 Feb 2005 18:20:11 -0500, Jim Witte wrote:

Is there a handler that I can put in that will be run if the user presses the stop button while running the script in the editor? (Yes, I know, this could be done quite easily with a compiled script and a dialog, but..)

The simplest method might be to put the whole script (or at least the entire contents of the run handler) in a try block - and then test for error number -128 ("user cancelled").


Remember, though, that hitting the cancel button of a dialog will also generate the same type of error. If you need to distinguish between a cancel resulting from a dialog and one produced by hitting the stop button (or pressing the 'command+.' keys), then you might have to resort to a little subterfuge.

One method is to put a space on either side of the word "Cancel" in the button specification:

------

display dialog "Blow up this computer now?" buttons {" Cancel ", "OK"}

------

...then take appropriate action (or rather inaction) if the button returned is " Cancel ". However, without using error number -128, it can sometimes be tricky to effectively prevent the script from continuing with some other action - especially in complex situations.

Another approach might be to wrap dialogs in their own, individual try blocks. A custom error number could then be substituted for any dialog cancellation - allowing the error to be handled separately by the main try block. With the entire script in a try block, it's also worth considering how best to flag any other errors that might arise.

Here's a rough example to demonstrate the sort of technique that I'm trying to describe:

------

repeat
try
try
if (display dialog "Choose \"Error...\" to force an untrapped error, or \"Continue...\" to keep the script running - leaving time to hit the 'Stop' button (or to press the Command+. keys)." buttons {"Cancel", "Error...", "Continue..."} default button 3 with icon 1)'s button returned is "Error..." then undefined_variable
on error number -128
error number 5001
end try
delay 2
on error e number n
if n is 5001 then
error number -128
else if n is -128 then
beep
display dialog "Ah-ha! You were caught red-handed stopping the script!" buttons ¬
{"Oops!"} default button 1 with icon 0
error number -128
else
error e
end if
end try
end repeat


------

---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: [OT] DeveloperTools Vs Xcode Tools VS X11
  • Next by Date: Re: do shell script without blocking?
  • Previous by thread: A 'Stop' handler for when script is run in editor?
  • Next by thread: ANN: Toggle Guest Access
  • Index(es):
    • Date
    • Thread