How to send an imessage text with applescript, only in provided service?

cre8eve picture cre8eve · Aug 4, 2012 · Viewed 56.5k times · Source

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!

Answer

Senseful picture Senseful · Oct 20, 2013

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