This is a situation for which I can’t find a solution.
The results of a handler stored in a script library are picked up by another handler to continue the flow.
In the first handler an error occurs; at that moment I want the execution of the script to end.
I tried this way:
try
handler1() -- an error occurs
on error
return
end try
-- At this point I want the script to end.
handler2()
But that did not work; the script is not interrupted and the handler2 continues its execution causing a cascade of errors.
Refine your question. Are you running a simple script, or a stay-open application where handler 2 is on idle() handler?
Because only in the 2nd case what you described should happen.
Simple script example (which terminates fine) on the return code line:
try
handler1() -- an error occurs
on error
return
end try
-- At this point I want the script to end.
handler2() -- here it doesn't executed at all
on handler2()
display dialog "The Script continue"
end handler2
on handler1()
return 1 / 0 --> overflow error should occur here
end handler1
.
. Stay-open script (should be tested as saved stay-open application). Doesn’t terminate on return code line:
try
handler1() -- an error occurs
on error
return -- here need quit {} command instead
end try
-- At this point I want the script to end.
on idle {}
display dialog "The Script continue"
return 5
end idle
on handler1()
return 1 / 0 --> overflow error should occur here
end handler1
You did not understand me. The quit {} command (with brackets), should be used only in the case of a stay-open application. Since you have a regular script, then it does not belong here.
Also, it became even more incomprehensible, for example, where did the try block of the original question go and some kind of errorWindow() handler appeared. What does it even do.
You are right, KniazidisR, when you say that I misunderstood your answer.
The change I made in my previous comment was because the try… end try block did not seem to me to be part of the core of the issue I want to raise.
The errorWindow() handler was only intended to more clearly state the example of an error situation whereby I want the script to stop and not run the rest of the code (in this case, and as an example, the searchClipWiki() handler).
For that reason, I mistakenly used the “return” command with the intention of terminating the script.
In short, my question is to know how I can end the execution of a script at a certain point in time.
DJUNQUERA. You can end execution of a script by use of error number -128. Example 2 appears to be most similar to your situation, except that the handler is in a script library (which still causes the calling script to stop).
EXAMPLE 1
dialogHandler()
display alert "You don't see this" -- this dialog is not displayed
on dialogHandler()
display alert "This is a test"
error number -128 -- script exits at this point
end dialogHandler
EXAMPLE 2
try
dialogHandler()
on error
error number -128 -- script exits at this point
end try
display alert "You don't see this" -- this dialog is not displayed
on dialogHandler()
display alert "This is a test"
error -- create error
end dialogHandler
EXAMPLE 3
tell script "AS Utilities"
testHandler() -- this handler contains error number -128
end tell
display dialog "You don't see this" -- this dialog is not displayed
Your solution works perfectly for the situation I’m interested in at the moment.
In addition, your examples are very interesting for a handler containing “error number -128”, because they allow its treatment by means of a try… end try block of the error and to channel which path to take, not being the only path the termination of the script .
I have located in “AppleScript Language Guide” 2 references to “error number -128”.
I will continue to study its use in other situations where it may also be of use to me.