Search code examples
applescriptimessageosascript

Send iMessage to group chat


How can you send a iMessage to a group chat? I have searched everywhere and I can't find anything. I want to send it to a group chat named something, possibly If I get a list of buddies in the chat I can send it to everyone at once? A problem may be that I'm also in the chat, so that might be a problem.


Solution

  • To send to an existing group chat, you will need to obtain the chat's internal guid. There are 2 methods of doing this.

    1. Extract it from the messages.app internal database, located on disk at ~/Library/Messages/chat.db. It is the guid column under the chat table. If you have the group chat named it will be easy to find by display_name.

    2. You can use an AppleScript handler for Messages with the on chat room message received handler to capture the ID when someone posts in that chat.

    Once you have the guid (should in a format similar to iMessage;+;chat831551415676550949), you can use AppleScript to send the message.

    tell application "Messages"
      set myid to "iMessage;+;chat831551415676550949"
      set mymessage to "Hello World"
      set theBuddy to a reference to text chat id myid
      send mymessage to theBuddy
    end tell
    

    To send an image, replace the mymessage text with:

    set ImageAttachment to POSIX file "File path here" as alias