26

Can somebody show me how to send a message directly to the user of iMessage via Messages app?

tell application "Messages"
    if service type of service of buddy whose name is ("Name here" is "iMessage") then set x to 1
    if service type of service of buddy whose name is ("Name here" is "iMessage") then set x to 2
    if service type of service of buddy whose name is ("Name here" is "iMessage") then set x to 3

    send "Message" to buddy "mail of name" of service x
end tell

I need to send a message to an account only via iMessage, not via google talk, AIM, bonjour. Thank you!

Andrew Mast
  • 311
  • 1
  • 17
cre8eve
  • 371
  • 1
  • 6
  • 11

6 Answers6

66

Instead of having to hard-code the iMessage service, you can find it automatically:

  1. Save the following script as sendMessage.applescript (Note: make sure to choose the Text option).
  2. Execute it by running: osascript sendMessage.applescript 1235551234 "Hello there!".
  3. This will send an iMessage to the phone number 123-555-1234 containing the text "Hello there!".

sendMessage.applescript:

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
Senseful
  • 86,719
  • 67
  • 308
  • 465
  • @fabian789 Thanks for the hint. Do you know a way around this limitation? Since you can send an iMessage to yourself (manually from Messages.app), is there no way to do this through a script? – Chris Apr 08 '14 at 10:46
  • 1
    @Chris No, I don't think that's possible – fabian789 Apr 09 '14 at 12:58
  • @fabian789 yes you can – Vitim.us May 10 '14 at 04:35
  • @Vitim.us How is this possible? – fabian789 May 11 '14 at 11:37
  • 3
    @fabian789 I tried to send to my iCloud email, and got the error `Messages got an error: Can't send a message to yourself.` so I just tried to send to another alias to my iMessage like my phone number, and it worked. – Vitim.us May 12 '14 at 22:05
  • Same here, it didn't work when sending to my email address alias, but it did sending to my phone number alias – Neil Dec 21 '14 at 17:11
  • @Senseful can help me with another problem? I need to determine is buddy have an iMessage before sending it (and buddy can be any number). We trying to make smart OTP system, that send iMessages and SMS as failback. In gui if i enter phone number its getting blue if phone have iMessage – Andrew Dryga Feb 13 '15 at 12:45
  • @Senseful Cool Script – Saurabh Saxena Apr 08 '16 at 22:54
  • This was working (sending to myself using my phone#) -- looks like a recent OSX upgrade broke that with `Can’t get buddy id "ABCDE123-1234-5678-90AB-123456789ABC:+441231234567". (-1728)`. Swapping back to my (not working before) email address for iMessage works fine now. – rcoup Jun 23 '17 at 09:12
  • 1
    would like to draw attention of you to the question with given by me `500` points bounty https://stackoverflow.com/questions/39027839/how-to-start-new-conversation-in-imessage-using-applescript – andilabs Oct 18 '17 at 11:58
  • If you want to hardcode the number into the script, put the number in quotes as is done in other answers. – Brady Dowling Nov 30 '18 at 00:02
  • Great script, my friend mad on me) – Anton A. Nov 01 '19 at 12:47
  • On Ventura, this crashes with "Expected class name but found identifier." highlighting the first "service" item – lfaraone Jun 07 '23 at 15:58
11

As far as I do understand it you cannot start a new conversation via AppleScript. Thus the Buddy and the service must fit together and must have an ongoing conversation.

If you have the name of the buddy you could do the following

tell application "Messages"
    get name of service of buddy "Buddy Name"
end tell

This will return the service name that fits to the buddy. Of course you could also use the the service id. But I like to use the name.

In the end you will be able to send a message with

tell application "Messages"
    send "Text of Message" to buddy "+43 555 XXXXXXX" of service "E:example@mac.com"
end tell
Touri
  • 119
  • 1
  • 4
  • 1
    To start a new conversation, follow this - http://stackoverflow.com/questions/39027839/how-to-start-new-conversation-in-imessage-using-applescript . Worked for me – mac Sep 09 '16 at 12:40
8

This script will send a message every 10~30 seconds

tell application "Messages"

    set targetBuddy to "+12025551234"
    set targetService to id of 1st service whose service type = iMessage

    repeat

        set textMessage to "Hi there!"

        set theBuddy to buddy targetBuddy of service id targetService
        send textMessage to theBuddy

        delay (random number from 10 to 30)

    end repeat

end tell
Vitim.us
  • 20,746
  • 15
  • 92
  • 109
  • Could you have a look at https://stackoverflow.com/questions/39027839/how-to-start-new-conversation-in-imessage-using-applescript – andilabs Oct 18 '17 at 11:59
4
tell application "Messages"
    set myid to get id of first service
    set theBuddy to buddy "mybuddy@me.com" of service id myid
    send "Washer Done!" to theBuddy
end tell

I had the same question and after some searching I found the answer. In order for the "first service" to be iMessage you will need to go into iMessage Preferences Accounts and re-order the iMessage account to be the first one. After that this works like a charm.

This will also start a conversation if there is not one existing.

Hope that helps!

DigiLord
  • 1,014
  • 1
  • 10
  • 17
3

Examples:

get services
get name of services
get buddies
get name of buddies 

Your line:

send "Test-Message" to buddy "buddy" of service "service"

seems to work if "buddy" and "service" is valid.

I have my iMessage with my Apple-ID registered, so when I execute "get name of services" I get for this service a string like

"E:myAppleID@host.com"

which I can use for "service". Buddy is just the name of your buddy, also as pure text. See "get name of buddies".

Hope it works!

osxgeek
  • 31
  • 1
0

Ok, I just made the following into an Automator action which grabs the logged in user´s full name, finds the matching iPhone number from Contacts, the service name, and lastly it sends the incoming text (from the previous action) to...myself on iMessage. Not very useful, at least to me at the moment, but I proved it to be possible in a way :)

set input to "Testing 123" **//This line is just for testing**
tell application "System Events"
    set MyName to get full name of current user
end tell
  tell application "Contacts"
      set myPhone to value of phone 1 of (person 1 whose name = MyName)   
      whose label = "iPhone"
   end tell
tell application "Messages"
    set MyService to get name of service of buddy MyName
    send input to buddy myPhone of service MyService
end tell
Andreas
  • 189
  • 1
  • 6