Saturday, April 29, 2017

#1 2004-09-24 05:07:53 pm

dishusa
Member
Registered: 2002-12-14
Posts: 92

Exiting Scripts

I have a script I am writing that is called from filemaker pro. During the running of the script, I would like to put in a way to exit the script if an error occurs. I cannot find an "exit script" command so would I just make the script into a handler and then have error  conditionally tied to a "return" statement?

Offline

 

#2 2004-09-24 07:41:14 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Exiting Scripts

Applescript:

error number -128

... will stop a script from executing any further. It's the same "User canceled" error that's generated by the "Cancel" button in most dialogs.


NG

Offline

 

#3 2017-04-19 10:42:11 am

Schmye Bubbula
Member
Registered: 2003-11-22
Posts: 93

Re: Exiting Scripts

How does one trap & exit a script from within a nested handler? The "Return" or "error number -128" only exits the handler, and the (explicit or implicit) main run handler continues unwanted. (I don't want a quit/return because I might be running it in a script editor, or in an applet I may want to stop the main run handler and go straight to an idle handler. Here's where I wish there were a GoTo command in AppleScript!)

Operating System: Mac OS X (10.6.8)

Last edited by Schmye Bubbula (2017-04-19 11:56:12 am)

Offline

 

#4 2017-04-19 12:38:04 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Exiting Scripts

Schmye Bubbula wrote:

The "Return" or "error number -128" only exits the handler

error number -128 stops the execution of the script. However, since it's an error, it'll be caught if it's in a 'try' statement or in a handler governed by one:

Applescript:

on run
   try
       stopTheScript()
   end try
   say "I didn't stop."
end run

on stopTheScript()
   error number -128
end stopTheScript

In this case, you need an 'on error' section to look out for and pass this particular error:

Applescript:

on run
   try
       stopTheScript()
   on error number n
       if (n is -128) then error number -128
   end try
   say "I didn't stop."
end run

on stopTheScript()
   error number -128
end stopTheScript

Or, if you don't need to do anything special with any other errors:

Applescript:

on run
   try
       stopTheScript()
   on error number -128
       error number -128
   end try
   say "I didn't stop."
end run

on stopTheScript()
   error number -128
end stopTheScript


NG

Offline

 

#5 2017-04-19 07:39:05 pm

Schmye Bubbula
Member
Registered: 2003-11-22
Posts: 93

Re: Exiting Scripts

^ Thanks, Nigel, for the instructive, detailed reply, above & beyond the call of duty! Thank you for your time!

I'm now straightened-out that error number -128 is "stronger" than resume. That would handle running things within a script editor, but I guess now what I'm really asking is whether there's a direct command or error handling routine to send the main run handler to the end of the script, skipping any interim lines, kind of like an EOF end of file, or "EOS end of script," so to speak, such that, e.g., an applet's idle handler could commence.

Offline

 

#6 2017-04-21 02:24:07 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Exiting Scripts

Hi Schmye.

return will take you out of a handler, returning the program flow to just after where the handler was called in the process which called it:

Applescript:

--on run
say "starting"
if (5 > 4) then return
say "continuing the run handler"
--end run

on idle
   say "idling"
   return 5
end idle

An alternative — and in some philosophies preferable — way would be to use the geography of the return condition:

Applescript:

--on run
say "starting"
if (5 ≤ 4) then
   say "continuing the run handler"
end if
--end run

on idle
   say "idling"
   return 5
end idle

If you want a handler to exit calmly following some error condition, put return in the on error statement:

Applescript:

say "calling the handler"
theHandler()
say "phew!"

on theHandler()
   try
       set fire to trousers
   on error
       say "oops"
       return
   end try
   say "false alarm"
end theHandler


NG

Offline

 

#7 2017-04-25 03:12:10 pm

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 2606

Re: Exiting Scripts

Schmye Bubbula wrote:

I don't want a quit/return because I might be running it in a script editor, or in an applet I may want to stop the main run handler and go straight to an idle handler. Here's where I wish there were a GoTo command in AppleScript!

Use user defined handlers then which are called from the Idle handler too.

Applescript:

on run
   try
       -- do something
   on error
       doIdleStuff()
   end try
end run

on idle
   doIdleStuff()
end idle

on doIdleStuff()
   
end doIdleStuff

GoTo commands are considered harmful. Jumping back and forth in code is done using loops, if statements and routines who are all auto-indexed for you. It's how modern programming languages are designed. Going back to the labeled jumps in code is everything the programming language is designed not to do, therefore it's evil and should never be used in any circumstances. Besides that programming languages who support GoTo cannot jump from one routine into another, they can only jump within the same routine because it's how the stack works. So at the end even GoTo wouldn't help.

Last edited by DJ Bazzie Wazzie (2017-04-25 03:55:56 pm)

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.138 seconds, 12 queries executed ]

RSS (new topics) RSS (active topics)