KM and Apple Messages

Is there a way to use KM to control Apple Messages?

I think my needs are simple…

I want to press a hot key to be able to bring Apple Messages to the front - then select my assistant’s name - then move to the text entry area. I get so many texts throughout the day and want a quick way to initiate a text to her quickly without having to find her in the list or manually search for her name.

Maybe something with Applescript?

Thanks!

Hey Sam,

That’s easy enough:

------------------------------------------------------------
set targetBuddyHandle to "myHandle@someDomain.com"

tell application "Messages"
  if not running then run
  activate
  set thisBuddy to first buddy whose handle is targetBuddyHandle and service type of service of it is iMessage
  set thisChat to make new text chat with properties {participants:{thisBuddy}}
  
  # set thisMessage to send "I am nuts!" to thisChat
  
end tell
------------------------------------------------------------

Uncomment the send line to actually send a canned message, otherwise it should bring up Messages ready to send to your specified Buddy.

It’s always good to do a little research:

I don’t use Messages much and never script it, so after looking at Messages’ AppleScript Dictionary I turned to Google:

Search String: AppleScript messages invite buddy to text chat

I learned a couple of things from the first two hits, but struck gold on the third:

https://macosxautomation.com/automator/dictate-send-message/index.html


Best Regards,
Chris

Hi Chris,

Thanks so much for the great info. I got it to work - and as you suggested, removed the last # line as all I want it to do is set the person and place the cursor in the text input field.

Any chance you can explain what this thing is doing?

Thanks again!

Hey Sam,

Open the Applescript Editor (or Script Editor on Yosemite), and paste this script into it.

------------------------------------------------------------
set targetBuddyHandle to "myHandle@someDomain.com"

tell application "Messages"
  if not running then run
  # activate
  
  set thisBuddy to first buddy whose handle is targetBuddyHandle and service type of service of it is iMessage
  
  # set thisChat to make new text chat with properties {participants:{thisBuddy}}
  
end tell
------------------------------------------------------------

Open the Event Log History window in the window menu. Make sure you’re showing Events and Replies in the event log window.

Run the script.

Uncomment (remove the ‘#’) the 2nd commented line (starts with ‘set thisChat’) and run the script again.

Now you get a little sense of what AppleScript is doing.

Now run this script:

tell application "Messages"
  handle of buddies
end tell

Now run this script with at least 1 chat active:

tell application "Messages"
  text chats
end tell

If you carefully read the language of the first script I wrote it basically tells you what it’s doing.

Set a variable named targetBuddyHandle to the handle name of the person you want to contact.

Make sure Messages is running and then activate it to bring it to the front.

(Ordinarily activate should be sufficient for this, but there’s been a bug for the last couple of versions of OSX to work around.)

set variable thisBuddy to the actual buddy-object of the buddy represented by the handle-name for use in the next command.

Make a new text chat object with 1 participant and assign it to variable thisChat.

The variable makes it easy to refer to the created text chat in other commands.

Clear as mud?    :smile:

Messages appears to be fairly scriptable, but it isn’t exactly straightforward to do so.

-Chris

Thank you so much again! - I will re-read your info - still not fully clear but the original script you sent works great and I have it working nicely within Keyboard Maestro!

So you could have a list of 1,000 mobile numbers and then get chat to send an SMS to each of them one by one? for each line repeat…?

I hope not. This would enable huge spam on SMS text msg.
There are services that provide SMS broadcast for legitimate uses, like schools, public safety, etc.

We have ***** clients and a massive opted in database for sms contact. I am just imagining the potential. How fast could it repeat 7,000 times i wonder…

set thisChat to make new text chat with properties {participants:{thisBuddy}}

Hey There,

What you’re missing is that the participants object is a list, so you can do this:

{participants:{buddy1, buddy2, buddy3, buddy4…}}

I suspect Messages would choke on 7000 list items.

I would experiment with 100 to start with. If it worked okay I’d add 100 at a time until it became problematic. Then I’d back off to a comfortably fast number.

After that you could iterate through n at a time.

If you were actually to iterate through 7000 numbers 1 at a time it would probably take a while.

The only thing to do is experiment and see what works.

-Chris