Get Contact First Name from Message?

Hello,

I’m new to Keyboard Maestro and have been pouring over the variables to find a way to do something that seems easy enough to me.

I would like to invoke a macro in an active Messages.app chat that will get the first name of the buddy in that chat and write something like ‘Dear FIRSTNAME, blah blah…’

I found an earlier thread discussing something similar, but it was for a specific person each time. I’m trying to get the first name of whoever I’m speaking to in the active chat.

Is KM able to handle something like that?

Thanks in advance.

There are no native KM Actions that provide access to the Messages app.
So, you will need to use AppleScript, or other scripting language.
Of course, you can invoke an AppleScript using the KM Action “Execute AppleScript

While AppleScript can provide response to, and get information from, a new incoming Chat message, it seems unable to get any information about existing Chats. But maybe someone with more AppleScript expertise, like @ccstone, will know of a way to do this.

I have done some research and testing, but have not found a solution.
You can try some Google searches like “AppleScript messages get properties of chat”, and do more research for yourself.

Good luck. If you do find a solution, please post here.

Hey @Ibnabouna,

Scripting in Messages is woefully broken…

tell application "Messages" to set chatID to first chat

try
   chatID / 0
on error errorMsg
   set chatID to errorMsg
   set AppleScript's text item delimiters to "\""
   set chatID to text item 2 of chatID
end try

--> "iMessage;-;name@domain.com"

This is as good as it gets with regular scripting in El Capitan and Sierra.

You can do some GUI-Scripting like this:

tell application "System Events"
   tell application process "Messages"
      tell (first window whose subrole is "AXStandardWindow")
         tell row 1 of table 1 of scroll area 1 of splitter group 1
            tell UI element 1
               set chatDescription to description
            end tell
         end tell
      end tell
   end tell
end tell

If your buddy is well enough named that might work for you.

-Chris

2 Likes

Chris, wow! :thumbsup:

Great job of getting “blood out of a turnip” :wink:


**EDIT:  2017-02-03  4:09 PM CT**

This works for the first chat in the list.
Any ideas on how to get the selected chat?

Hey JM,

This should work:

-------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/02/03 16:24
# dMod: 2017/02/03 16:24 
# Appl: Messages & System Events
# Task: Get the Description of the Selected Chat
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Get, @Description, @Selected, @Chat
-------------------------------------------------------------------------

tell application "System Events"
   tell application process "Messages"
      tell (first window whose subrole is "AXStandardWindow")
         tell table 1 of scroll area 1 of splitter group 1
            tell (first row whose selected is true)
               tell UI element 1
                  set chatDescription to description
               end tell
            end tell
         end tell
      end tell
   end tell
end tell

-------------------------------------------------------------------------

-Chris

1 Like

Thanks Chris.
That works great.


**To everyone:**

The buddy/contact (or phone# if not a contact) is in the first sentence:

>Chris Stone. Last message: Okay, I figured it out.  Will post on the forum in a sec. 4:25 PM.


As you can see, this returns the following fields within the Description text:

* Buddy Name (or  phone#)
* Text of Last Message
* Time of Last Message

Wow, that is amazing. I was working on an AppleScript, but couldn’t figure out why it wasn’t working. It seems many aspects of parsing info from the Buddy variable are broken.

I didn’t consider GUI scripting at all, but to my amazement, this last script almost works perfectly. I say ‘almost,’ because it returns the full buddy name (first and last) and the content/time of the last chat, which I don’t need.

Is there a way to simply return the first name of the other person in the active chat?

Thanks for all of the effort you put into this

I see now that the script is pulling AXDescription from the active window, which is great. If it will be difficult to parse out just the first name from AXDescription, would it be possible to run the result through a RegEx that would strip out everything from the first space to the end? That would isolate the first name from AXDescription.

While I can conceptualize it, I am not proficient enough to actually implement it.

Yes. The below macro, which uses @ccstone's AppleScript, parses the results of the script.


2017-02-04 14:25 CT

  • Fix the RegEx to account for the Chat Date as a text item
  • Fix the RegEx to account for unusual character at start of phone number

###Example Results


###MACRO:   Get Buddy/Contact/Phone from Selected Message @Example @Regex @Messages

~~~ VER: 1.1    2017-02-04 ~~~

####DOWNLOAD:
Get Buddy-Contact-Phone from Selected Message @Example @Regex @Messages.kmmacros (12 KB)


###ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Get Buddy Name from Selected Chat in Messages app.
    • If Buddy is not a Contact, the phone# (or other reference#) is returned.

HOW TO USE:

  1. Select a Chat in the Messages app.
  2. Trigger this macro

WHAT IT DOES:

  1. Using an AppleScript GUI Script (by @ccstone), get the Chat Description.
  2. Use RegEx to extract:
  • Buddy Name
  • Last Message
  • Message Time

TAGS: @Messages @Chat @Buddy @RegEx @AppleScript



###AppleScript (from @ccstone)

-------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/02/03 16:24
# dMod: 2017/02/03 16:24 
# Appl: Messages & System Events
# Task: Get the Description of the Selected Chat
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Get, @Description, @Selected, @Chat
-------------------------------------------------------------------------

tell application "System Events"
   tell application process "Messages"
      tell (first window whose subrole is "AXStandardWindow")
         tell table 1 of scroll area 1 of splitter group 1
            tell (first row whose selected is true)
               tell UI element 1
                  set chatDescription to description
               end tell
            end tell
         end tell
      end tell
   end tell
end tell

return chatDescription
-------------------------------------------------------------------------

Thanks, @JMichaelTX! This is great... I follow exactly how you take ATXDescription, parse it down to variable, and then further parse out the name into first and last name.

Unfortunately, when I run the unmodified macro, I get the following error:

Seems something is wrong with the RegEx somewhere?

Yep. I forgot to account for sometimes the Messages app will show the date/time as a relative term, like "Yesterday".

So, in this Action, change the RegEx to:
^(.+?)\. Last message: (.+\.) ([\w ].+)\.$

AND, in this Action, change the RegEx to:
^.?\+

I will update my above post with the macro shortly.

Just updated macro in above post.

Thanks, @JMichaelTX... it works! Kinda. lol.

I can't make heads or tails of it, but in some chats, it works absolutely perfectly, but in others, I get the same message:

I've been trying to find out what's different between the chats in which it works and those in which it doesn't, but so far, I don't see a difference.

Does it work on all of your Messages.app chats, including the much older ones?

Please post the Chat Description of one or two of the Chats that do NOT work.

  • You can get the Chat Description by viewing the KM Variables in KM Preferences, and selecting the “GBC__Desc” variable.
  • It should look something like this:

Chris Stone. Last message: Okay, I figured it out. Will post on the forum in a sec. Yesterday.

.

Yes, it worked on all of the Chats I tested.

I’m running Messages 9.2 (5100) on macOS 10.11.4.
What macOS are you running?

Okay, here are some in which the macro did NOT work:

Sandy Pachit. Last message: Will do! 2/2/17.

Caroline Azeandar. Last message: I will definitely tell him to come! Thank you so much for checking up on the both of us 2/2/17.

I do see that one has punctuation at the end of the message whereas the other doesn’t, but both of them failed.

FYI, I am using Messages.app 10.0.0 on OS X 10.12.4 Public Beta

Sorry for the bugs, but sometimes RegEx can be a real bear, especially when the source data can vary so much.

Please try this RegEx in the below Action:
(?i)^(.+?)\. Last message: (.+\.?) (\w[^AMP].+?)\.$

OK, just tested both of those, and they seem to work with the RegEx I just posted above.
Please let me know if it works for you.

Sorry for what? You’ve been so helpful and generous with your time.

That worked perfectly! I’ve been randomly testing various chats, and so far, no issues. I’ll keep testing.

I don’t know how to thank you for all of your efforts.

1 Like

Hey Guys,

Rule-of-the-thumb – when using regular expressions try to use the simplest possible expression.

If all you need is the first word of a sentence then just look for that and nothing else.

^\w+

In practice that may not be enough, because a sentence or a word may contain punctuation (but you get the general idea).

Okay, let’s apply some more magic:

-------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/02/03 16:24
# dMod: 2017/02/04 18:43
# Appl: Messages & System Events
# Task: Get the Description of the Selected Chat and set text of message field.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Get, @Description, @Selected, @Chat
-------------------------------------------------------------------------

tell application "System Events"
   tell application process "Messages"
      tell (first window whose subrole is "AXStandardWindow")
         tell splitter group 1
            tell table 1 of scroll area 1
               tell (first row whose selected is true)
                  tell UI element 1
                     set chatDescription to description
                  end tell
               end tell
            end tell
            set theSalutation to "Dear " & first word of chatDescription & " – How you be?"
            tell text area 1 of scroll area 4
               set focused to true
               set value to theSalutation
            end tell
         end tell
      end tell
   end tell
end tell

-------------------------------------------------------------------------

This script pulls the first word out of the description – assembles a salutation – and emplaces it into the message field automagically.

-Chris

Yep, that is definitely a good rule.

But I was trying to provide a more general solution, that not only provided the Buddy’s first name, but also:

  • Buddy Full name
  • Buddy First Name
  • Buddy Last Name
  • Last Message
  • Date/Time of last message.

Reminds me of a favorite saying of an old boss:
“Better is the enemy of good enough.”

I was about ready to revert to a simple solution of only Buddy First Name, when I finally (I hope) figured out the RegEx for the more complete solution.

Also, by working on this, I learned some new stuff, and reinforced some old stuff.
Thankfully, @Ibnabouna was patient and kind enough to suffer through testing of my various solutions.