Page 1 of 1

Sending an iMessage using the Mac OS X Messages app

PostPosted: Thu Jul 16, 2015 10:27 am
by jay (support)
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.

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

PostPosted: Fri Dec 30, 2016 11:59 am
by JaceJenkins
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


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

PostPosted: Thu May 18, 2017 11:27 am
by NewfD90
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!

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

PostPosted: Thu May 18, 2017 11:53 am
by roussell
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

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

PostPosted: Thu May 18, 2017 12:38 pm
by NewfD90
Thanks, but that's what I'm doing. The image will not send as it's waiting for the other end to accept:

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

PostPosted: Thu May 18, 2017 12:55 pm
by roussell
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

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

PostPosted: Thu May 18, 2017 1:32 pm
by NewfD90
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!

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

PostPosted: Thu May 18, 2017 2:50 pm
by jay (support)
<sarcasm>One of the many reasons I LOVE AppleScript... </sarcasm>

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

PostPosted: Fri Dec 08, 2017 7:44 am
by McJohn
The above script send 2 separate messages.
What is the solution for 1 message with the picture and tekst?

Kind regards,

John