When ending handler start running part way through script

Hi,

I was wondering if it was possible to run a handler and depending on the outcome of the handler (through if statement) where it starts running again in the main body of the script.

I have a handler that checks for label data stored and if the user wants to print any remaining labels from that data.

The only problem I have is that it runs at the beginning of the script but when it finishes it runs at the beginning of the script this is because if no data has been left over (all labels have been printed) it runs the script as normal.

Is there a way to do this at all?

Thanks,
Bruce

There is no way you can jump in code if that is what you mean. E.W. Dijkstra has written down in a small paper why jumpers (goto) shouldn’t be allowed in any programming language other than assambly. In short, it’s a filthy hack in the programming language design which never should be allowed to use. So, I think, that’s the reason why it’s not implemented in AppleScript because it is not difficult to enable.

Anyway, you have currently a wrong workflow and therefore you think you need to jump back and forth in code. Everything should be able to control by loops. This means that when you have something like

set username to display dialog "What is your username?" default answer "<username>"
set userpassword to display dialog "What is your password?" default answer "123456" with hidden answer

if checkCombination() = true then
	--need to jump to end of script
end if

if checkUserName() = true then
	--need to jump back to line 2 (set userpassword to ...), only ask for new password not for username
else
	--need to jump back to line 1 (set username to), the username is invalid so we start over
end if

As the code above is very clear what it should do if such thing as goto were available, but it’s also clear that my code shows bad programming. A jump up in code is like a loop a jump down is like an if-statement. So to make the code above work we’re going to use loops:

set checkResult to false --AppleScript doesn't allow me an double exit repeat
repeat --auto indexed label
	set username to display dialog "What is your username?" default answer "<username>"
	repeat --auto indexed label
		set userpassword to display dialog "What is your password?" default answer "123456" with hidden answer
		set checkResult to checkCombination()
		
		if checkResult or not checkUserName() then
			exit repeat --goto line after goto instruction
		end if
	end repeat --goto label line 4 instruction (line 2 in previous script)
	if checkResult then
		exit repeat --goto line after goto instruction
	end if
end repeat --goto label line 2 instruction (line 1 in previous script) 

NOTE: it could be written down shorter but this gives an good idea how it works the same as the script above

The result is the same workflow but this time using the correct AppleScript controls, automatically created goto (jumpers).