Here with another project that has me stuck. I do not have a macro example to show because I don't know how this will work, all I can do is tell the scenario and share what it could look like.
I will be getting an email that will be used to trigger a macro. I will need some information from these emails. The email will look like this:
From: mike@mike.com
Subject: XYZ Termination - John Smith [152637]
(Message Content)
John Smith [152637] has been terminated for XYZ on 02/07/2023.
Please update the necessary fields.
Thanks.
What will change and what I need:
Form the Subject line or Content:
XYZ - Set to local_entity
152637 - The numbers will always appear between [ ] - Set to local_empID
02/07/2023 - set to local_termDate
I have tried the regex you all have taught me over time but could not get it to work. I think mainly because what I used in the past was when the content was one line or one after the other in a column.
If there is something more I can offer or pieces of a macro that might help make things easier for you, please let me know. Thanks!
When you select a sample message in Mail and run this AppleScript from Apple's anemic Script Editor app does it extract the entire text of the message?
tell application "Mail"
set selectedMessageList to selection
if selectedMessageList ≠{} then
set selectedMessage to item 1 of selectedMessageList
tell selectedMessage
its content
end tell
else
error "Zero messages were selected!"
end if
end tell
I was using a script you and Nige put together for me a while back that always works (once I change the specific details such as KM #, variable, etc). And setting a rule in apple mail for the subject line.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
my doKeyboardMaestroMacro(eachMessage's content)
end repeat
end tell
end perform mail action with messages
end using terms from
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on doKeyboardMaestroMacro(msgContent)
tell application "AppleScript Utility"
«event coredosc» "5E50DFA0-06E7-4D56-8B86-ACC8D67CFC20" given «class KMpa»:msgContent
end tell
end doKeyboardMaestroMacro
You might have been getting stuck on the fact that [ and ] are special characters in RegEx and need to be "escaped" to match their literals. Something like this should do you:
...though you'll have to change Local_theText to whatever your variable holding the email's contents is called.
\[(\d+)\] has been terminated for (.*) on (\d{2}/\d{2}/\d{4})
The RegEx is longer than it needs to be, but it might be best to be overly cautious when dealing with canning notifications!
There's nothing dumb about not being able to spot invisible characters! But it is something you learn to check for after getting caught out bazillion times yourself...
As you get more comfortable with those it becomes easier to stretch and reach for more understanding.
Note that in the first search action I've used the multi-line flag (?m), because I'm not assuming there will be only one line of data from the email.
From there I've broken each task into a separate action.
Pro Tip – always test your regular expressions in a good programming editor like BBEdit, but be aware that most of them don't use strict ICU regex like the macOS and Keyboard Maestro.
BBEdit uses PCRE for instance, and the differences can give newbies headaches.
I used to keep this critter around for testing, and it uses ICU regex:
I can't try out the latest version, because it requires macOS Monterey – but I suspect it's improved, since I last had my hands on it.
Thanks Chris, I am now reading this because I finally have the time to absorb it. I have been taking regex courses to learn some basics. This is very helpful.