Sending an iMessage using the Mac OS X Messages app

Posted on
Thu Jul 16, 2015 10:27 am
jay (support) offline
Site Admin
User avatar
Posts: 13164
Joined: Mar 19, 2008
Location: Austin, Texas

Sending an iMessage using the Mac OS X Messages app

Because there is no direct API to send Apple iMessages, you have to script the Messages app in Mac OS X (Mountain Lion and later I believe). The following script should work well:

Code: Select all
set targetMessage to "Some message to some target iMessage user"
tell application "Messages"
    set targetService to 1st service whose service type = iMessage
    set targetBuddy to buddy "INSERTIDHERE" of targetService  # see discussion below for what identifier to use
    send targetMessage to targetBuddy
end tell


The real trick is figuring out how to identify the buddy. Sometimes, the account you want to send to is identified by their phone number, including the + at the beginning: "+19875551234" or whatever. Other times, it's registered with their email address: "someemail@somehost.com" or whatever. The best way to see is to run the following script :

Code: Select all
tell application "Messages"
   buddies of (1st service whose service type = iMessage)
end tell


This will return a list of all the accounts that can receive iMessages. The buddies will look something like this:

buddy id "86ABE32D-7CCF-4116-BD3D-0C49F84932FA:someemail@somehost.com" of application "Messages"

or

buddy id "86ABE32D-7CCF-4116-BD3D-0C49F84932FA:+15055551234" of application "Messages"

The bolded part is what you want to use in the script above to identify the buddy.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Dec 30, 2016 11:59 am
JaceJenkins offline
Posts: 21
Joined: Oct 16, 2015

Re: Sending an iMessage using the Mac OS X Messages app

If you want to send a group text edit the script like this:

Code: Select all

tell application "Messages"

        activate
   
           set theBuddy1 to buddy "1st Contact Name In Address Book or phone number or email address"
           set theBuddy2 to buddy "2nd Contact Name In Address Book or phone number or email address"
   
           set thisChat to make new text chat with properties {participants:{theBuddy1, theBuddy2}}
           set thisMessage to send theMessage to thisChat

end tell


Posted on
Thu May 18, 2017 11:27 am
NewfD90 offline
Posts: 46
Joined: Mar 17, 2017

Re: Sending an iMessage using the Mac OS X Messages app

This works great for text. However, I'm trying to send a JPG image as well. iMessage tries to send it, but it sits there and waits on the the recipient to accept the file. Meanwhile, on the recipient side, there is no option to accept, so only the text portion shows up.

Anybody figured out a way around the accept?

Thanks!

Posted on
Thu May 18, 2017 11:53 am
roussell offline
User avatar
Posts: 777
Joined: Aug 18, 2008
Location: Alabama

Re: Sending an iMessage using the Mac OS X Messages app

NewfD90 wrote:
This works great for text. However, I'm trying to send a JPG image as well. iMessage tries to send it, but it sits there and waits on the the recipient to accept the file. Meanwhile, on the recipient side, there is no option to accept, so only the text portion shows up.

Anybody figured out a way around the accept?

Thanks!


Make sure you use POSIX file instead of POSIX path of file
Code: Select all
set theAttachment to POSIX file "filename"


So, something like this should work:
Code: Select all
tell application "Messages"
   set theAttachment to POSIX file "File path/name"
   set theBuddy to buddy "BUDDY ID" of service "SMS"
   
   send "This is a text message" to theBuddy
   send file theAttachment to theBuddy
   
end tell


Terry

Posted on
Thu May 18, 2017 12:38 pm
NewfD90 offline
Posts: 46
Joined: Mar 17, 2017

Re: Sending an iMessage using the Mac OS X Messages app

Thanks, but that's what I'm doing. The image will not send as it's waiting for the other end to accept:
Attachments
Untitled.png
Untitled.png (109.21 KiB) Viewed 743 times

Posted on
Thu May 18, 2017 12:55 pm
roussell offline
User avatar
Posts: 777
Joined: Aug 18, 2008
Location: Alabama

Re: Sending an iMessage using the Mac OS X Messages app

Just had a second to connect to the house and look at the exact script I use. This runs every time the doorbell is pressed so I get multiples every week and know it works. Make sure you have your destination saved as a contact, I don't remember the exact error I got before doing that, but it wouldn't work until I created a contact for whomever I send a message. Also, the Indigo Server has it's own Apple ID/iMessage login. The only real difference form the example above is that I use iMessage instead of SMS, but I just tested the script above and it works too.

Code: Select all
tell application "Messages"
   set theImage to POSIX file "/Users/Indigo/Data/Cameras/Frontdoor/Images/Latest.jpg"
   set theService to first service whose service type = iMessage
   set theBuddy to buddy "+1205XXXXXXX" of theService
   
   send "Front Door Doorbell Pressed" to theBuddy
   send file theImage to theBuddy
   
end tell


Terry

Posted on
Thu May 18, 2017 1:32 pm
NewfD90 offline
Posts: 46
Joined: Mar 17, 2017

Re: Sending an iMessage using the Mac OS X Messages app

Sweet! It works now; thanks for that.

My code was identical to yours, with the exception of the word "file" (I had send theImage to theBuddy).

What a difference that one small word made.

Thanks again!

Posted on
Thu May 18, 2017 2:50 pm
jay (support) offline
Site Admin
User avatar
Posts: 13164
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Sending an iMessage using the Mac OS X Messages app

<sarcasm>One of the many reasons I LOVE AppleScript... </sarcasm>

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests