tell application "Messages"
repeat with adressee in contacts
set theContact to contact of adressee
send theMsg to participant theContact
set myDelay to (random number from 1 to 10)
delay myDelay
end repeat
end tell
I get these error message:
random number from 1 to 10
--> error number -1708
«event ascrgdut»
--> error number -1708
random number from 1 to 10
--> error number -10004
On the following iterations it work fine.
Any ideas why?
If I break out the code and just make a loop like this:
repeat 5 times
set myDelay to (random number from 1 to 10)
display dialog "Random number: " & myDelay
delay myDelay
end repeat
set kontakter to {{kontakt:"abc", hex:"x63"}, {kontakt:"+def", hex:"x64"}, {kontakt:"kfb", hex:"x65"}, {kontakt:"+ghi", hex:"x65"}, {kontakt:"jkl", hex:"x66"}}
tell application "Messages"
set myDelay to 0
repeat with adressat in kontakter
try
set theHex to hex of adressat
set theKontakt to kontakt of adressat
-- log "theKontakt: " & theKontakt
set theMsg to msg & "
" & theHex
--send theMsg to participant theKontakt
--tell application "System Events" to set myDelay to (random number from 1 to 10)
tell me to set myDelay to (random number from 1 to 10)
--delay myDelay
--log (current date)
tell me to log (current date)
on error errMsg
log "error: " & errMsg & "theKontakt: " & theKontakt
end try
end repeat
end tell
When I run this I get this interesting error messages in the first iteration:
tell application "Messages"
random number from 1 to 10
--> error number -10004
end tell
tell current application
random number from 1 to 10
--> 1
end tell
tell application "Messages"
current date
--> error number -10004
end tell
tell current application
current date
--> date "Wednesday, 29 January 2025 at 19:47:12"
end tell
In the second iteration and on it is slightly different:
tell application "Messages"
(*date Wednesday, 29 January 2025 at 19:47:12*)
random number from 1 to 10
--> error number -10004
end tell
tell current application
random number from 1 to 10
--> 7
end tell
tell application "Messages"
current date
--> error number -10004
end tell
tell current application
current date
--> date "Wednesday, 29 January 2025 at 19:47:12"
end tell
Gonna try with tell current application now.
What is current application inside a tell-statement? Because I am in a tell application "Messages" but the error messages indicates that that fails when I create a random number or date, but then it succeeds when telling current application, which I would’ve guess is Messages??
This is super weird. I copied the whole script to a new “document” and executed it. No errors. Of course, I tried again with the old script - still errors. I “diff:ed” them, diff couldn’t find any differences. Copied back and forth a few times, with the same result - errors in the “old” version, no errors in the “new” version.
I agree with Ed that the errors are probably a result of sending the random number command to the Messages app. Normally these errors are handled automatically, as demonstrated in the following screenshot:
I don’t have any way to test this, but it’s possible that the script has timing issues that are related or unrelated to the other errors. This can be tested for by temporarily deleting the random number command and setting the myDelay variable to fixed values of varying lengths.
Ed’s suggestion appears to avoid the errors altogether:
I reorganized the script because my code was stupid
on run ()
set theMsg to "Hello world"
set contacts to {{contact:"mailaddress", hex:"x9B"}, {contact:"mailaddress", hex:"x9C"}, {contact:"phonenumber", hex:"xA7"}, {contact:"phonenumber", hex:"xA8"}}
repeat with adressee in contacts
try
set theHex to hex of adressee
set theContact to contact of adressee
set theMsg to msg & "
" & theHex
tell application "Messages"
send theMsg to participant theContact
end tell
set myDelay to (random number from 3 to 6)
delay myDelay
on error errMsg
log "error: " & errMsg
end try
end repeat
end run
I STILL get an error when generating the random number during the first iteration.
The error I’m getting from your code is that the variable msg isn’t defined, which is true. The best thing to do would be to make the second line of the script:
It allows you to set the ‘default target’ for your code.
For example, if it was set to ‘Finder’ in an empty script, you could just type some code and it would be handled like it was in a Finder tell block. So running set xy to selection would return whatever files were selected in the Finder.
It can be turned on or off in Script Editor’s General preferences.