Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: detecting a command-period
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: detecting a command-period



On 9/9/03 8:51 AM, "Jonathan Reff" <email@hidden> wrote:

> Is there a way to detect if the user cancels out of a running process in a
> Studio app with a command-period? I'd like to be able to stop progress bars,
> update buttons, etc. The process involves "do shell script" commands, and then
> going into a repeat loop until the commands are carried out. Command-period
> does cancel, but I'd like to know if the user has done this.

See if a normal applescript 'quit' handler works:

on quit
-- do all your cleanup stuff
continue quit
end quit


However, 'quit' handlers are rather unexpected things. 'continue quit',
which you'd think would always just do the quit, instead puts you back where
you were. If you were in a repeat loop, it finishes the repeat loop - not
what you want. The solution - at least in regular stay-open applets - is to
have a global variable - or a property reset to default when launching
(which happens automatically with Studio apps since properties aren't
retained between launches) - which your repeat loops and sundry other
locations check up on, and then reset the value in the quit handler. If its
value has been changed by the quit handler, then do an 'error number -18'
which quits without more roundabouts. Like this:

property quitNow : false

(* or:
global quitNow
set quitNow to false
*)

--do stuff
if quitNow then error number -128 -- really quits

repeat with i from 1 to whatever
if quitNow then error number -128 -- catches the 'continue quit'
-- do stuff
end repeat

--more stuff
if quitNow then error number -128

--etc.

on quit
-- do all your cleanup stuff
set quitNow to true
continue quit
end quit



------------

If you any of the script sections in which 'error number -128' appears are
encased in try/error blocks, you have to make an exception for this error:

try
--blah
repeat --
-- as above
end repeat
on error errMsg number errNum
if errNum = -128 then
error number -128 -- really quit!
else
display dialog errMsg & return & return & errNum
-- whatever else you want to do
end if
end try

--
Paul Berkowitz
_______________________________________________
applescript-studio mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-studio
Do not post admin requests to the list. They will be ignored.

References: 
 >detecting a command-period (From: Jonathan Reff <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.