Debug Assistance - Stumped By Inconsistency

I created the below macro to reformat macOS' mail response.

Refomat To Outlook_as At 20260122 copy.kmmacros (37.7 KB)

I trigger the macro by selecting a Mail in my Inbox and pressing one of teh triggers, there is one trigger for each of Reply, Reply All and Forward.

The macro works inconsistently in two different ways:

  1. The below action works inconsistently. More recently the action is failing more often than not

More confusing to me is the following:

a) %MailRawSource% is one of macOS Mail's tokens.

b) Earlier and later actions include the below two which work consistently and flawlessly, where %MailCCRecipients% and %MailSender% are also macOS Mail tokens.

Why do %MailCCRecipients% and %MailSender% work flawlessly while %MailRawSource% does not, noting the action / assignment is the same in all three?

Worth also noting is that I cannot use the %MailContents% in place of %MailRawSource% as I need the additional information in %MailRawSource%.

  1. The second set of actions that works inconstantly are selecting macOS Mail's menu bar items Message > Reply, Reply All, and Forward per the below.

The error message I get is:

When I check the Message > Reply, Reply All, Forward menu items I never see them disabled.

Would appreciate assistance at getting this sorted as well.

Thank you.

Can you try a test? Run this on various selected emails:

Mail Contents Test.kmmacros (3.8 KB)

Image

Even if the first "Display" is blank, does it consistently show the raw contents in the second?

I'm wondering if you've a slow connection to your mail server, and the token is timing out when trying to retrieve uncached raw contents -- recipients and sender will be downloaded as Mail updates the mailbox, but email contents are often left for later or until needed.

(I think, but don't know, that a timed-out token will return an empty string rather than erroring out your macro. Anyone?)

Interesting idea.

Tested ~ 15 times.

When the 1st window was filled then so was the 2nd window.

When the 1st window was not filled then neither was the 2nd window.

There was never and instance where the 1st window was not filled and the 2nd window was filled.

Is there any consistency as to when it's blank? That is, is it always the same emails?

-rob.

Completely random and no pattern.

In fact, the same e-mail may work / not work on once instance and 2 minutes later be exactly the opposite.

The other puzzling thing is the Menu Bar references not working!

Since it sounds like it's not a headers-only issue, another reason I can think of that covers both of these symptoms is that you're losing the mail selection for some reason. Or, if the "Forward" always works and it's only "Reply" and "Reply All" that are failing you might have multiple messages selected, potentially exceeding the amount of dat that can be returned by the %MailRawSource% Token.

The submacros are missing from your upload, but they don't get called until after the failing Token so I don't think they'll be the cause.

Since everything's working as expected in my tests (Mail in macOS 26.1, using both Exchange and standard IMAP accounts) I reckon @griffman's right -- you'll have to troubleshoot this yourself, stepping through the macro with the Debugger so you can see exactly what's going on.

I feared that.

I am still working but will reboot my computer later and see whether than clears things up.

I went through debugging and it sheds no light -- at least to me -- as it simply pops up the same empty display text box or error notification.

Greatly appreciated, will keep everyone posted.

Subroutines attached for those interested.

__Mail Macros.kmmacros (84.1 KB)

I found the solution to both problems.

I have no idea why this is a problem but am hoping one of you can explain it to me.

I will explain this in steps,

  1. As a start, here is the entire macro with all its bits.

__Mail Macros.kmmacros (121.7 KB)

  1. I decided to start with the simplest problem, the Mail menu commands Message > Reply was not working.

  2. I decide to take the Message > Reply action and move it up the macro one action / step at a time until it fired. The action that I moved up is:

  1. I discovered that the problem was in this block and, in particular the Display Text action that reads

....which in the larger sense of things appears here in the macro

  1. If I leave that action enabled and place the Message > Reply action above it then the Reply message appears. It works. :grinning_face_with_smiling_eyes:

If I leave the action enabled and place the Message > Reply action below it then the Reply message fails. It does not work! :grimacing:

If I disable the Display Text box as I currently have then the macro fires reliably, without issue!

  1. So, the question I have for @Nige_S , @griffman, @peternlewis and others is simple, what problem is being caused by the Display Text action that it is responsible for i) the %MailRawSource% token not being received and the Message > Reply / Reply All / Forward not firing?

I will for now leave the Display Text action disabled but I would love to know what the problem and solution are.

Look forward ti your assistance, feedback and ultimately a solution!!

Thank you!!

I don't know why technically, but what happens is that the Display Text window blocks the ability of Keyboard Maestro to "see" what's in the Mail window. If you add a long enough pause for you to click the OK button to dismiss the window before the Mail menu command runs, it should work.

As a longer term solution, use a Notification or write the value to a text file, etc. Or just use the debugger when needed.

-rob.

1 Like

@griffman Agree on workarounds.

Although I thrilled that my macro is working again, I would still like to know the cause and why it only happens with the %MailRawSource% token.

I guess @peternlewis would be the one who might be able to answer that :).

-rob.

Agreed which why I included him. Hopefully he will chime in.

The error message is telling you the problem.

2026-01-28 13:01:46 Select Menu Item failed because the menu item Message➤Reply is disabled.

When you execute the Display Text action, it has focus, the Mail window losses focus, and the menu items are disabled.

@peternlewis

Appreciated and thank you.

Three quick follow ups:

  1. Why does the Raw Data fail to load? Is it the same reason, loss of focus?

  2. Why does the problem only occur with the Display Text window for the %MailRawData% token but none of the other Display Text windows?

  3. Would it be correct to conclude based on the above that when testing macros Display Text windows are not ideal because they can take away focus and impede a macro’s functionality? What is the preferred Display Text format for debugging?

Thank you.

Yes. I would probably append to a log file, and then display that, either with tail -f in the Terminal, with the Console, or BBEdit.

MailRawSource is simply AppleScript for:

tell application "Mail"
	set r to ""
	if it is running then
		try
			set s to the selection
			set m to first item of s
			set r to (source of m) as string
		end try
	end if
	r
end tell

What you get back is what Keyboard Maestro gives you for the token.

No idea, that is two specific a case for me to have any idea what is happening.

There's a lack of version info in your posts (or I'm just not seeing it!) but the things you're having trouble with all work fine in macOS 26.1 -- %MailRawSource% doesn't need Mail to be the frontmost app and your "Select Menu" Action should work fine because you've specifically targeted Mail.

I still think there's something going here on that's specific to your setup...

Try stepping through in the Debugger again, but pay less attention to the Debugger and KM variables and more to what happens with Mail itself, particularly on whether email remains selected during those parts of the macro.

1 Like

I am running macOS 26.2 on an Apple M4 MacBook Pro.

I noticed that the Display Text windows (i.e., any Display Text window) takes the focus away from the Mail specific e-mail (i.e., when I execute the macro, the selected e-mail is highlighted in blue but when the Display Text box appears, the selected e-mail is highlighted in grey [focus is lost]).

I played with it some more and noticed that each and every Display Text box placed before the Messages > Reply / Reply All / Forward caused the same behaviour. It is not related to the %MailRawContents% token but to the Display Text Box.

I think that while the reference to Mail in the Select Mail is correct as you note that one also needs to have an e-mail selected which is not the case because of the loss of focus and why we are seeing this behaviour in Mail (and not elsewhere) (i.e., it is specific to needing an e-mail selected).

Comments / thoughts?

One not-so-quick update later...

Not quite. It takes "input focus" away from Mail -- keystrokes are directed at the "Display Text" window -- but Mail remains the frontmost app. Importantly, the selected email remains selected in Mail, which is what's needed for both the Token and the menu manipulation.

  • Mail does not need to be the frontmost app and does not need "input focus" for the %MailRawSource% Token to be evaluated
  • Mail does not need to be the frontmost app for a targeted (rather than "Select menu in Front Application") "Select or Show a Manu Item" Action to work

In the KM Editor (so KM is the frontmost app) try "View"ing the "Display Text" Action

...and "Try" the "Select 'Reply'..." Action

...and they should both work.

In short -- I can't reproduce the issue here so it looks to be something specific to your macro and/or setup rather than a general KM/Mail problem.

As usual, you really need to write and then post the most minimal macro that will demonstrate the issue. Nine times out of ten just writing a new demo macro and the explanatory post will lead to a face-palm moment when you realise what you were/won't doing in your main macro that caused the problem -- what's known as "Rubber Ducking".

1 Like

Appreciate the clarification and the fine point on "input focus" versus "frontmost".

I would love to know and understand the issue, full macro posted below.

__Mail Macros.kmmacros (126.2 KB)

As usual, you are correct.

Apologies for not created a new / purpose built macro to test things and then post them. I will do so from this point onwards!

Point taken!

1 Like

Hardly the "minimum macro to demonstrate the problem".

But if I re-enable the "Display Text"s and put a "Cancel This Macro" after displaying the reformatted date, the macro works fine for me whether or not the selected email has someone CCed -- the presence or absence of a "Display Text" makes no difference to the %MailRawSource% Token.

For the future, don't forget you can use Tokens directly in many places -- you don't have to set to a variable first. So:

...can be replaced with

...avoiding the subroutine altogether.