Programmatically send text message through Messages app on OS X 10.10

With OSX 10.10 you can now send and receive text messages through your phone, from your mac. Is it possible to do this programatically? Either via applescript or a reverse engineered API?


Solution 1:

Using:

tell application "Messages"
    get name of every service
end tell

I noticed that I have "SMS" as a 4th option (yours may be different). So then I used:

launch application "Messages"
tell application "Messages"
     activate --steal focus

     set targetBuddy to "12345550123"
     set targetService to id of service "SMS"
     set textMessage to "Just a test"

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

This lets me send a text message over SMS Relay. I'm not 100% sure that I did this correctly, but it does work if targetService points to "SMS". Now to convert it to script that works from the terminal.

Solution 2:

There is quite a few good examples out there for using Applesccript to send iMessages. I haven't found one yet that will work to a number via sms, the below script only works with iMessage. Also it limited to the number(s) in your contacts(buddy list) and will error out if the buddy/number isn't in your contacts. Here's one that I use to send notifications of a server backup complete.

Create an applescript named "sendMessage.scpt" with the following code:

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

Then from terminal.app run this command to send iMessage:

osascript /path/to/sendMessage.scpt 12345550123 "Your Text Message to Send"

just change 12345550123 to the number your sending to.

Link to original guide from stackeoverflow

Solution 3:

send.scpt

on run {phoneNumber, message}
    tell application "Messages"
    send message to buddy phoneNumber of service "SMS"
    end tell
end run

run from terminal like this:

osascript send.scpt +48500123456 'hello andi!'

this works both with iMessage and standard SMS when you have enabled on your iPhone the option Text Message Forwarding by setting your e.g macbook device.