Stopping a big script in the middle?

I have a question of style, and I know this might require a lengthy reply. So please bear with me and thanks ahead of time if you take time to reply.

I have (several) big scripts that do a lot of different functions, many of them involve making PDFs from InDesign and copying them to various places. I need to have a way to know if there’s been an error in some step and be able to cancel out/stop/escape out of the main function, but also be able to do something when it errors out. Several of my steps are function calls, but some things are just steps in order to be done in the main function, too small to break out (I feel). How do I register if there has been an error in any sub function and then drop out of the main function? Is it just a matter of nesting many levels of Try/On Error? I have one script that checks about 9 times if a variable “myContinue” is True, and script continues if it is true, or skips to the end if it is false. But this requires placing that “if myContinue is true…” statement all over the place. Plus if there is an error inside of the function I still need to be able to bail out and register that too.
I guess I’m looking for a more simplified way to detect this kind of situation. Would there be something similar to “do while…” statement that scans the trueness at every single step of the function?

Here’s what I have devised now:

global myContinue

on clicked theObject
set myContinue to true
if myContinue is true then
--do some stuff
--set some global variables
end if
if myContinue is true then
tell app "InDesign"
--do some stuff
end tell
end if
if myContinue is true then
doAFunction()
end if
if myContinue is true then
doSomeOtherFunction()
--do some stuff
doMoreFunction()
end if
if myContinue is false then
--do some error messages or clean up routines
end
end clicked

on doAFunction()
	--do some things
	--set some global variables
	--return myContinue as True or False
end doAFunction

Does anyone have any suggestions to streamline this sort of code some more?

I think what your looking for is something like this:



set x to true -- needs to be boolean

repeat while x

-- function1
-- function2

end repeat

I am not sure if this is exactly what you wanted … but it could help. The thing is that you can’t apply the “while” to your own functions. Ie, something like this is not possible:



set x to true

doThis() while x

end doThis

Just thinking…

Hi,

if you want to abort the whole process when the first error occurs, you can simply use a try block.
Errors, which occur in a handler, will be returned, too.
You can force an error with the error command.


on clicked theObject
	try
		--do some stuff
		--set some global variables
		tell application "InDesign"
			--do some stuff
		end tell
		doAFunction()
		doSomeOtherFunction()
		--do some stuff
		doMoreFunction()
	on error
		--do some error messages or clean up routines
	end try
end clicked
try

	
on error errorMessage
	
	log "Error occured with message: " & errorMessage
	-- log the error so you know what caused it.
	
end try

I found the log and errorMessage very useful, especially if you have many tries.