Sometimes multiple-passes are the easiest way to manage complex text:
Input text:
"Jim Underwood. Last message: Okay, I figured it out. Will post on the forum in a sec. Yesterday."
** REQUIRES the Satimage.osax AppleScript Extension to be installed.
------------------------------------------------------------------------------
set chatDescription to cng("\\. *last message: *", "\\n", chatDescription) of me
set chatDescription to cng(" (\\w\\S+)\\.$", "\\n\\1", chatDescription) of me
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on cng(_find, _replace, _data)
change _find into _replace in _data with regexp without case sensitive
end cng
------------------------------------------------------------------------------
Output text:
"Jim Underwood
Okay, I figured it out. Will post on the forum in a sec.
Yesterday"
Now I have 3 specific fields in a text record to work with.
From there I can validate the text structure and move forward:
------------------------------------------------------------------------------
set nameField to paragraph 1 of chatDescription
if fndBool("(\\w+) (\\w+)", nameField, false, true) â false then
set {firstName, lastName} to (words of result)
end if
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on fndBool(_find, _data, _all, strRslt)
try
find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
return true
on error
return false
end try
end fndBool
------------------------------------------------------------------------------
Yep. That fails if the Buddy is NOT a Contact, and then the phone# is used like this:
+1 (123) 123-1234
So here's the RegEx I used for a simple extract of Buddy First Name: ^(.+?)(?: |\.|$)
This will get the first word (if more than one), also works for all of these cases:
BFirstN only on the line
BFirstN.
BFirstN BLastN
+1 (123) 123-1234
So, I've revised my macro to first do the simple RegEx Search (above), which should be much more fault tolerant (rarely fail).
Then, I attempt the full Search to get the other data.
But I've set the Action to NOT fail if it doesn't have a match.
I like using the KM RegEx facility because it is easier to setup and see the results, and I don't have to bother with getting/setting KM Variables in script.
I can confirm that, so far, @JMichaelTXâs macro is working perfectly, and itâs nice to know that, in the future, I have access to variables for the last name, message, and timestamp, as well.
@ccstoneâs solution is also working perfectly for the first name. It doesnât need Keyboard Maestro or any third-party app, but I found it worked well with Typinator and TextExpander.
Iâm really happy to see this community so active in pushing Mac automation to the masses. This is really powerful stuff that can change and improve whole workflows. At a time when Apple seems less interested in these things, itâs reassuring to meet professionals like @JMichaelTX and @ccstone who are willing to help.
I begin many of my iMessages using Address Book, which results in chatDescription being âNew message to FIRSTNAMEâ as opposed to what we were working with above.
Thankfully, ccstoneâs AppleScript was easy enough to understand, so I modified it with an If/Then statement to account for both chatDescriptions: if the first word of chatDescription is âNewâ, then select the fourth word; else, select the first word.
# 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
if first word of chatDescription is "New" then
set theSalutation to fourth word of chatDescription
else
set theSalutation to first word of chatDescription
end if
end tell
end tell
end tell
end tell
-------------------------------------------------------------------------
Help! MacOS 10.15.4 broke this AppleScript, which I've been happily using for these three years since this thread. I now get the message "AppleScript error (#-1719): System Events got an error: Canât get row 1 of table 1 of scroll area 1 of splitter group 1 of window 1 of application process "Messages" whose subrole = "AXStandardWindow" whose selected = true. Invalid index."
Thank you to anyone who was going to help, but it seems like the issue was caused by a wayward AppleScript. A system reboot fixed the problem. Blessings
Wondering if there is a update for Monterey
Getting the error
System Events got an error: Canât get splitter group 1 of window 1 of application process "Messages" whose subrole = "AXStandardWindow". Invalid index.
Here is the code that has always worked from me. This gets the raw info from the window, but then you have to parse it out to extract the first name.
tell application "System Events"
# Get the frontmost app's *process* object.
set frontAppProcess to first application process whose frontmost is true
end tell
# Tell the *process* to count its windows and return its front window's name.
tell frontAppProcess
if (count of windows) > 0 then
set window_name to name of front window
end if
end tell