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: 18199
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: 51
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: 61
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: 1108
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: 61
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 14240 times

Posted on
Thu May 18, 2017 12:55 pm
roussell offline
User avatar
Posts: 1108
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: 61
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: 18199
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

Posted on
Fri Dec 08, 2017 7:44 am
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

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

The above script send 2 separate messages.
What is the solution for 1 message with the picture and tekst?

Kind regards,

John

Posted on
Tue Oct 30, 2018 4:35 pm
petematheson offline
Posts: 847
Joined: Sep 14, 2014
Location: Southampton, UK

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

Hi Guys,

So I have an issue with this.
I'm sending a snap of a CCTV camera to 2 different people.
I've tried this with 2 separate applescripts, 1 combined script, and have 1-2 second delays, but STILL I get an issue where I will be sent 2 photos instead of one.

set driveway_pic to "/Users/HomeServer/Downloads/driveway.jpg"
tell application "SecuritySpy"
capture image camera name "Driveway" as driveway_pic with overwrite
end tell
delay 1
tell application "Messages"
set DestFile to POSIX file "/Users/homeserver/Downloads/driveway.jpg"
set theService to the first service whose service type = iMessage
set Pete to buddy "+xxx" of theService
set Erica to buddy "+xxx" of theService
send "Motion Detected: Driveway camera" to Pete
send file DestFile to Pete
send "Motion Detected: Driveway camera" to Erica
send file DestFile to Erica
end tell

Literally in the Messages file attachments window I can see it sending to Me, Erica then Me again.

???

Posted on
Wed Oct 31, 2018 2:54 am
GlennNZ offline
User avatar
Posts: 1555
Joined: Dec 07, 2014
Location: Central Coast, Australia

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

Coincidentally I have just posted this Plugin

viewtopic.php?f=222&t=21520

Which should manage this plus much more!

Glenn

Posted on
Wed Oct 31, 2018 3:07 am
yassi offline
Posts: 468
Joined: Sep 06, 2015
Location: Germany

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

Glenn,
the new plugin looks cool, I have to try it. Thx.

But in general how do you guys handle this with the iCloud account? Do you all have an iCloud account for Indigo?
Will an alias work too, or it has to be a completely separate account?
Glenn is mentioning in the "Basics" of the new plugin: "Need to be signed into Messages/iMsg on Indigo Mac with ideally separate indigo account".
If so, then I need another device (smartphone) with the corresponding iCloud to get the iMessages.

Yassi

Posted on
Wed Oct 31, 2018 6:05 am
siclark offline
Posts: 1960
Joined: Jun 13, 2017
Location: UK

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

Yes. I have a house iCloud account that my imessgages comes from. Also useful for the find friends mini plugin as well.


Sent from my iPhone using Tapatalk

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest