Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript to send new iMessage

Hey guys - I'm trying to create an apple script to send a new iMessage. But I'm stuck. Can anybody help?


tell application "System Events"

tell application "Messages" to activate

tell process "Messages"

click menu item "New Message" of menu "file" of menu bar 1

end tell

...

endtell


... and thats it. How Do I tell Messages to fill in an email adress or phone number now and to send it?


Thank you!

Mac Pro (Early 2008), OS X Mountain Lion (10.8.2)

Posted on Sep 21, 2012 3:05 AM

Reply
Question marked as Best reply

Posted on Sep 21, 2012 8:18 PM

The best way is to not use UI Events at all - they're cumbersome at best. Instead consider the direct approach - tell Messages what you want it to do (send a message) not necessarily how you would do it (click here, type this, etc.)


For example, to send a message to a buddy you can:


tell application "Messages"

set theBuddy to buddy "someone@mac.com" of service "iCloud"

send "Hi there" to theBuddy

end tell


(adjust for your appropriate service name, of course.

22 replies
Question marked as Best reply

Sep 21, 2012 8:18 PM in response to iMavy

The best way is to not use UI Events at all - they're cumbersome at best. Instead consider the direct approach - tell Messages what you want it to do (send a message) not necessarily how you would do it (click here, type this, etc.)


For example, to send a message to a buddy you can:


tell application "Messages"

set theBuddy to buddy "someone@mac.com" of service "iCloud"

send "Hi there" to theBuddy

end tell


(adjust for your appropriate service name, of course.

Oct 1, 2012 12:54 PM in response to Camelot

thanks for the help Camelot

i followed your steps and got a service id for icloud which looks like this


tell application "Messages"

set theBuddy to buddy "+15555555555" of service id "F7273233-267B-4EB6-851D-49B440B18411"

send "hi there" to theBuddy

end tell


would that service id work on all computers that the script is run?

or is that unique by computer? is so, how can i make it universal?

i can not get it to work by naming my service "iCloud" only.


note* im using the phone number instead of the email, i did noticed that when using messages, the conversation has to be started already in order for the "buddy" to be recognized

Oct 2, 2012 9:21 AM in response to epi_jimenez

would that service id work on all computers that the script is run?

or is that unique by computer?

Service IDs are unique per computer, so that's not going to work on any other system.

is so, how can i make it universal?

Well, don't use ID 🙂.

If you want a script that will work on ANY machine then you have a problem since there is no persistent identifier. You can reference any service by a number of parameters (e.g. ID, name, index value, etc.) but there's no guarantee that any value will be the same on any other machine - even the 'name' may be different, and the index value may be different depending on how many accounts the user has setup.

i can not get it to work by naming my service "iCloud" only.

Is that the name of your service? I'm guessing not. Amend my earlier script to 'get name of every service' if you want to see the names you can use. This still may not be consistent across machines, though.

i did noticed that when using messages, the conversation has to be started already in order for the "buddy" to be recognized

That shouldn't be the case. Is the remote user in question in your buddy list? If he isn't then you can't reference a buddy by name since he doesn't exist on your system until a conversation is in progress. If a buddy is in your buddy list then you should be able to reference them by name without an active conversation.

Aug 25, 2014 1:58 AM in response to iMavy

i wanted to know this because if you take this line of code

tell application "Messages"

set myid to get id of first service

set theBuddy to buddy "+15555555555" of service id myid

send "Hi there" to theBuddy

end tell and put run after it u get an imsg/sms spammer example


tell application "Messages"

set myid to get id of first service

set theBuddy to buddy "+15555555555" of service id myid

send "Hi there" to theBuddy

end tell

run

and it sends about 100 messages in 1 second 😀

AppleScript to send new iMessage

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.