user7903682
user7903682

Reputation: 319

Can't send message to group chat

I found a script that sends messages from the command line, but It won't work with my group chat with spacing and capitalization in the name.

#!/bin/sh
recipient="${1}"
message="${*:2}"
echo "$recipient"
cat<<EOF | osascript - "${recipient}" "${message}"
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
EOF

when I run ./sendmessage "GROUP CHAT" "test" it gives me 228:261: execution error: Messages got an error: Can’t get buddy id "7087805D-ED73-4DB9-81AB-7C964B98AB34:group chat". (-1728)

Upvotes: 1

Views: 754

Answers (1)

Zeke Snider
Zeke Snider

Reputation: 496

Unforunately, it is not possible to send a message to a group chat with applescript by using the chat's name. To send to an existing group chat, you will instead 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), your script can be modified to send to the ID instead:

#!/bin/sh
recipient="${1}"
message="${*:2}"
echo "$recipient"
cat<<EOF | osascript - "${recipient}" "${message}"
on run {targetChatID, targetMessage}
   tell application "Messages"
    set targetChat to a reference to text chat id targetChatID
    send targetMessage to targetChat
   end tell
end run
EOF

To send an image instead of text, replace the targetMessage with:

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

Upvotes: 1

Related Questions