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