Stopping a script for Messages don't stop until loop is fully executed

I have a list with about 60 contacts/recipients (contacts) I want to send an SMS/iMessage to. Below is a simplified version of my script. Note the 30 seconds delay between each message.

I just executed this in Script Editor and after the first message was sent I realised I made a mistake with the content of that message and therefore clicked Stop. The script was in (had been for maybe 10-20 seconds) the loop that looked for an UI element. What happened when I clicked stop was that UI-element loop immediately exited and then the script looped over the remaining 59 entries in contacts, completely ignoring both the UI-element lookup loop as well as the 30 seconds delay, instead delivering the erroneous message to all recipients.

This has happened several times/all times I have clicked Stop in the middle of the execution of this script.

What is going on here? Why doesn’t the script immediately exit when I click Stop? (In fact, I think it takes a few seconds for it to stop, like something is queued and is blocking input c.f., low level CPU interrupts and have to finish before it can react on Stop). And why does it execute the outer loop despite that I have clicked Stop? And ignoring the both the UI-lookup loop and the delay while doing so?

It is like the script somehow is executed in Messages independent of what is going on in Script Editor.

on run
	repeat with adressee in contacts
		try
			tell application "Messages"
				send theMsg to participant adressee
				tell application "System Events" to tell process "Messages"
					repeat while exists UI element 3 of group 2 of group 2 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of window 1
						delay 1
					end repeat
				end tell
			end tell
			delay 30
		on error errMsg
			log "error: " & errMsg
		end try
	end repeat
end run

I cleaned it up a bit…

on run
	repeat with adressee in contacts
		try
			tell application "Messages" to send theMsg to participant adressee
			tell application "System Events" to tell process "Messages"
				repeat while exists UI element 3 of group 2 of group 2 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of window 1
					delay 1
				end repeat
			end tell
			delay 30
		on error errMsg
			log "error: " & errMsg
		end try
	end repeat
end run

Yes?

Is that supposed to make a difference?

No , not really. I just have a dislike for nested tell blocks. I try to avoid them as much as possible.

The reason yours looped over the rest was because the try block caught the stop as an error (error # -128, errMsg:“User canceled.”). If you want it to stop completely if it gets an error, then try this…

on run
	repeat with adressee in contacts
		try
			tell application "Messages" to send theMsg to participant adressee
			tell application "System Events" to tell process "Messages"
				repeat while exists UI element 3 of group 2 of group 2 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of window 1
					delay 1
				end repeat
			end tell
			delay 30
		on error errMsg
			log "error: " & errMsg
			return -- I added this to stop completely.
		end try
	end repeat
end run

or if you only want it to stop if the user clicks stop then try this…

on run
	repeat with adressee in contacts
		try
			tell application "Messages" to send theMsg to participant adressee
			tell application "System Events" to tell process "Messages"
				repeat while exists UI element 3 of group 2 of group 2 of group 1 of group 1 of group 1 of group 1 of group 1 of group 1 of window 1
					delay 1
				end repeat
			end tell
			delay 30
		on error errMsg number errNum
			log "error: " & errMsg
			if errNum = -128 then return
		end try
	end repeat
end run