Search code examples
applescriptimessage

Error in AppleScript for iMessage - Can’t get service of text chat id


I get the error: Can’t get service of text chat id "iMessage;-;". (-1728) - when trying to send a text to another imessage client on a mac (different user). Here is the code fragment where it is failing:

using terms from application "Messages"
    on message received theText from theBuddy for theChat
        # get what we need
        set recvService to name of service of theChat

Thx.


Solution

  • Update: As explained below, the behavior observed is a bug. However, the OP himself found a workaround: the from argument of the message received event (theBuddy in the sample code) is an instance of the Buddy class, which also has a service property, which functions correctly:

    set recvService to name of service of theBuddy # instead of the buggy `of theChat`
    

    Note: The message received event's for parameter (theChat in the OP's code) is an instance of the text chat class, which inherits from the chat base class. If the Dictionary Viewer in Script Editor isn't configured to show inherited items, it will not be obvious by looking at the text chat class that it indeed does have a - currently buggy - service property. To make inherited items visible, open Script Editor's Preferences dialog and check Show inherited items.


    It appears there's a bug in the AppleScript support for Messages.app (observed on OSX 10.11.1); I suggest you file a bug at http://bugreport.apple.com; here's a simple demonstration:

    tell application "Messages"
    
        set svc to first item of services
    
        set cht to first item of (chats of svc)
    
        # !! Breaks, even though it should return the same object as `svc`.
        # "Can’t get service of text chat id \"iMessage;-;<recipient>\"."
        service of cht 
    
    end tell
    

    It seems that trying to access any property of a text chat instance is currently broken.

    You can try a workaround:

    # Given a chat instance, returns its originating service.
    on getService(cht)
        local svc, cht
        tell application "Messages"
            repeat with svc in services
                try
                    (first chat of svc whose it is cht)
                    return contents of svc
                end try
            end repeat
        end tell
        return missing value
    end getService
    
    # Sample invocation
    set recvService to name of (my getService(theChat))