`Send iMessage` Action: 2023 reliability and alternate methods

In 2023, are there still reliability issues with sending iMessages via AppleScript?

ie:

:warning: Warning: there have been cases where Messages sends the message to the wrong recipient. Do not send overly sensitive messages and verify delivery.

Via: actions:Send Message [Keyboard Maestro Wiki]

In my quick test (Monterey 12.6, KM 10.2), I found that the following (via Chris Pennington – Send iMessage With AppleScript) seems to work reliably:

on run argv
  tell application "Messages"
    set targetBuddy to "(XXX) XXX-XXXX"
    set targetService to id of 1st account whose service type = iMessage
    set textMessage to ( item 1 of argv )
    set theBuddy to participant targetBuddy of account id targetService
    send textMessage to theBuddy
  end tell
  log "Message sent"
end run

I wonder:

  • Is this fundimentally the same as what Keyboard Maestro is using to pass information to Messages.app?
  • It would seem that targeting the Phone Number or Email Address of a contact would have less chance of sending to the wrong recipient. But I don't know why this "bug" occurs.

I see that the Messages Suite (in Script Editor.app) has a few ways to target what should be a unique ID:

participant n : A participant for an account.  syn buddy
elements

contained by application, accounts, chats.
properties

id (text, r/o) : The participant's unique identifier. For example: 01234567-89AB-CDEF-0123-456789ABCDEF:+11234567890
account (account, r/o) : The account for this participant.
name (text, r/o) : The participant's name as it appears in the participant list.
handle (text, r/o) : The participant's handle.
first name (text, r/o) : The first name from this participan's Contacts card, if available
last name (text, r/o) : The last name from this participant's Contacts card, if available
full name (text, r/o) : The full name from this participant's Contacts card, if available

But again, because I don't know what the fundamental issue is, maybe all of these methods are affected and unreliable.

Thanks.


Related threads:

Potentially, yes.

Messages AppleScript can return entirely the wrong buddy information in certain circumstances.

However the change in 10.2 should ensure that it either fails or goes to the correct person. But it's hard to make promises given the odd behaviours I've seen in Messages’ AppleScript support.

Even if you target them with their phone number, email address or uuid* ?

*IOW: Something more than their name, which is where I was assuming the failure was happening (since two discrete contacts could have the same first/last name). But I could be wrong.

Under the hood, is KM doing essentially the same as this script? And in addition KM correlates the name to its unique id when you select someone from the Contact dropdown (in the Send iMessage action)?

Bummer and understood.

I mentioned "in 2023" because I wasn't sure if Apple's Catalyst re-write of Messages.app has had a known impact on its AppleScript consistency.

I don't know, I just found cases where it gave entirely the wrong information.

Essentially, yes.

1 Like