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

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

PostPosted: Tue Oct 30, 2018 4:35 pm
by petematheson
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.

???

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

PostPosted: Wed Oct 31, 2018 2:54 am
by GlennNZ
Coincidentally I have just posted this Plugin

viewtopic.php?f=222&t=21520

Which should manage this plus much more!

Glenn

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

PostPosted: Wed Oct 31, 2018 3:07 am
by yassi
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

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

PostPosted: Wed Oct 31, 2018 6:05 am
by siclark
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