Sending a new iMessage fails with "Expected class name but found identifier"

Using the highly-rated answer from StackOverflow How to send an imessage text with applescript, only in provided service?:

on run {targetBuddyPhone, targetMessage}
    tell application "Messages"
        set targetService to 1st service whose service type = iMessage
        set targetBuddy to buddy targetBuddyPhone of targetService
        send targetMessage to targetBuddy
    end tell
end run

On Ventura, this errors at save with "Expected class name but found identifier." and highlights the first instance of "service". How should the script be corrected?

Is there a difference in grammar between releases? This code works on Big Sur.

Accepted Reply

I’m not familiar with the backstory here, but on macOS 13 the class you’re looking for is called account, not service. So this code works:

tell application "Messages"
    set targetAccount to first account whose service type is iMessage
end tell

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • The example you gave also provides an error "Expected class name but found identifier" with the word "account" highlighted.

  • (I'm on an M2 Pro, if that's relevant)

  • Nvm, I revived my machine and this works now.

Add a Comment

Replies

I’m not familiar with the backstory here, but on macOS 13 the class you’re looking for is called account, not service. So this code works:

tell application "Messages"
    set targetAccount to first account whose service type is iMessage
end tell

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • The example you gave also provides an error "Expected class name but found identifier" with the word "account" highlighted.

  • (I'm on an M2 Pro, if that's relevant)

  • Nvm, I revived my machine and this works now.

Add a Comment