Macro Not Doing Anything Once Triggered by AppleScript for Mail

Hello Community,

Having some trouble... if anyone has a solution, TIA.

Also... I tried to keep as much of the info in the macro that wouldn't be too descriptive for work security purposes. Other than file paths and names, everything is the same as the actual macro I have been using.

Mac Mail triggers the macro when the correct subject line email comes in. The wheel spins on the KM Engine for a second and then stops. Nothing else happens. This worked last week...

Mac Monterey Version: 12.6.3

TS Generator 2 copy.kmmacros (37 KB)

This is the AppleScript I have set.

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


on doKeyboardMaestroMacro(msgContent)
   tell application "Keyboard Maestro Engine"
      do script "E65DDFA3-899F-47CA-BE93-663BE9E11A7A" with parameter msgContent
   end tell
end doKeyboardMaestroMacro

Verify that the macro is being triggered (you can do that by looking in the Engine.log file (Help ➤ Open Logs Folder) or by using the Interactive Help, or by adding some debugging action to the start of the macro (like a Speak Text action)).

Assuming it is running, I would add a Debugger Breakpoint This Macro action to the start of the macro, and then use the macro debugger to step through and see if you can spot where things do not behave as you expect.

1 Like

As with all "it was working then but not now" situations, start by asking yourself "What's changed?". OS/app updates, macro edits, maybe the format of the emails you're receiving?

With all those pauses, if the Engine only runs for a second then stops I'd expect the problem to be a failed search somewhere in the first six actions -- except they all have "Notify on Failure" turned on, which should have already given you a clue. Have you blocked KM's Notifications?

Further on, this action in "Entity" (and there's similar in "Entity 2") looks suspicious:


...because I'd expect to see more of a path there. At the very least I'd expect a file name extension, since you seem to be treating it as a spreadsheet!

Also note that, as written, there's a chance (however slim) that neither the "Entity" nor "Entity 2" groups execute -- if no document opens, what will the "Save As" action be aimed at? Possible unintended consequences there as the keystrokes will target whatever app is frontmost at the time.

1 Like

OS Update is definitely something new here. But if that is the case, how do I address it? Revert back to older version?

Yes, so this is part of the example redaction. In my script there is a path there that opens an xlsm file. The purpose of the macro is -
There are 2 different entities. Each entity has different clients. Each client has their own time sheet for the worker.
So based on the variables set from the email, the macro should - Entity -> Client -> Timesheet -> Edit timesheet with information found in variables -> Name appropriately based on variables -> save to gdrive folder.

All that to say, there is a path in my actual macro, I took it out to avoid sensitive information on the forum.

And it looks like I do have the notifications turned off.. I will change that now.

I took @peternlewis 's advice and checked the logs... the issue is in my RegEx.

2023-02-10 10:05:29 Search Regular Expression failed to match (?m)^Class: (.*)$. Macro “TS Generator 2” cancelled (while executing Search Variable “Local_NHF” Using Regular Expression (ignoring case) - CLASS).

This one is for "Class" - but before this I tried it and it crashed for Email. I disabled email and it crashed for Class.

Do you see anything wrong in my RegEx?
Keyboard Maestro Actions (3).kmactions (4.9 KB)

So it may be that your AppleScript is getting different data from the email than before, because Mail has been updated as well -- try adding a "Display Text in a Window" action containing %Variable%Local_NHF% at the top of the macro so you can inspect the data.

It's not crashing -- it's aborting because it can't find a match in Local_NHF (your email content). Check the emails carefully in case there's been a change in what you're being sent.

It may be that these are validly-empty "fields". By default, "Search using Regular Expression" aborts the macro when it can't find a match. You can change that in each action's "Options" menu, but you should decide what you want to happen if the search draws a blank -- do you want to leave the cell blank in the spreadsheet, put in "N/A" or similar, do something else? Different resolutions will suggest different methods for handling the situation.

1 Like

I put a "Display Text" and it shows the email content. So that seems right.

For the next part, if something draws a blank, I want it to kick an error message to my email (I just made this up now since you brought up a great point). So I will also need to figure out how to send an email with error.

I am also testing "Display Text" after each RegEx. The issue begins at CandidateEmail but I can't find an issue between the email content and the variable it is pulling.

It's not just "is the content there" -- it's "is the content you expect to be there, there". So if the problem is the "CandidateEmail" regex, do you have a line in Local_NHF that starts with "CandidateEmail: ", making sure that no space has crept in at the beginning of the line, between "Candidate" and "Email", etc.

Note that it doesn't matter if the "CandidateEmail" is blank -- the search still works, even though Local_Email will be set to an empty string. But it does matter that being blank also removes the space character after the : because then the search won't work. If that is the issue then change your regex to

(?m)^ClientName: ?(.*)$

...where that added ? makes the preceding space character optional.

The "Send Mail Message" action is easy enough, but where you put it and how you use it will depend on what you want to happen. Should the macro abort, or still fill in whatever it can to the spreadsheet? Do you want to include the "name" of the missing field in the email you want to send out?

But, really, this is shutting the stable door after the horse is far over the horizon. IIRC you are getting this data from a form -- that's the place to catch missing data, by making fields mandatory. It's a better experience for you -- no missing data! -- and a better experience for the user because they don't have to go back to the form and do it all again.

1 Like

To add to your understanding of the situation -
A co-worker enters a macro in ZenDesk that has this information. The macro is set to be exactly what I need for KM (Local_NHF and RegEx format). When co-worker hits "Submit" in ZenDesk, it emails me the response of
Entity: YTR
CandidateName: Mike S
ClientName: Changa
Class: Taco Eater
PaylocityID: 12345

Oh snap. I think you solved it. The ? in the regex allowed the message and variables to go through properly. More importantly, it showed the problem. There was an extra space between CandidateEmail: and the email. Same with ClientName. Running it now correctly to see if that was the issue.

1 Like

The ? wouldn't help with an extra space -- you'd have two spaces', and ? means "zero or one space". Extra spaces should get included in the .* anyway, so CandidateName: Mike S would set the CandidateName variable to Mike S.

Your macro works fine with the data you provided above, as you can see from this stripped down version (and try some extra spaces in the first action's text box to see what happens then):

TS Gen 2 Stripped.kmmacros (7.6 KB)


The data you gave is, presumably, the data you are expecting to get. That works, while the email contents don't -- implying that the data you are getting is not what you expect!

Aahh it worked. Not sure what happened or what was made different, but it works. And imagine it's because of the two of you. Thank you.

But of course, two other things came up that maybe you can answer/help with.

Referencing this macro, email content, variables - Towards the end of the Excel part, you'll notice the naming scheme of the file when I save it.
To make it perfect, it needs to be 2023 Entity Timesheet - Client - Last name, First name - Classification

2023 WYT Timesheet - Chimi - Rhino, Mike - Taco Eater

Is there a way to split the "Worker Name" variable before putting it into the Save title? Or another option is to split it in the beginning. So a way to save the first string to Local_First and after the space second string to Local_Last?


It could look something like this:
%ICUDateTime%yyyy% %Local_Entity% Timesheet - %Local_Last%, %Local_First% - %Local_Class%

Is this the answer: RegEx Split by Capital Letters

Edit: These examples in the link above work if it was one long string, not two words.

There be dragons...

If a worker inputs "Elena Maria Sampa" (to take a munged colleague's name), do you split that to "Elena Maria" and "Sampa" or "Elena" and "Maria Sampa"?

Far better, if possible, to let them (or your colleagues) make the decision by having "First Name(s)" and "Last Name(s)" as separate fields at input.

Otherwise you are going to have make some rules based on your expected data. "Split on spaces" would be the usual one for us, because "double-capped" surnames like "Jones-Smythe" are more common than "Jones Smythe".


Ha having my co-workers. change their approach, not a shot. To the first part, I would want it to be Sampa, Elena Maria. Is building that catch doable? Well actually, I can set it to 3 variables. And then create the naming schema in it's own task. Kinda of like if Local_Name3 does not contain a value, ignore it. It's 7am on a Saturday here.. so I haven't looked into how that translates to KM.

And I agree with your double-capped comment. So that would all be captured appropriately.

Lastly, my co-workers know that macros are not perfect 100% of the time. They would inspect the title before sending it out. And for Random outliers with 3+ names, they would just correct if it is wrong.

Which is fine, unless their surname is actually "Maria Sampa"! That's what I mean about dragons -- whatever your rule it will be wrong some of the time, so you have to pick "least worst" based on your expected input.

But if you are assuming everything before the last space is forename(s) and everything after the surname:


Which would, of course, break if your client was Douglas Fairbanks Jnr :wink:

1 Like

You've done it again. Thank you so much. I wish I knew a better way to thank you for this. You are awesome.