Macro: Unread Mail Menu

Back when Mail plugins were a thing I was a longtime user of one called Mail Unread Menu. Its primary function was a visual indicator of new mail in the menu bar. Badges were customizable and, while not important to me, it offered a small menu of Mail-related abilities.

For me, it was all about that menu bar badge since I keep the Dock hidden, usually move between apps using Keyboard Maestro hot keys, and don’t use the built-in Mac notifications. This little utility saved me switching to Mail to see if new mail had arrived.

It finally dawned on me that I could use KM to replace this lost functionality since KM offers the ability to have the Macros in a Macro Group display in the menu bar. This capability, for anyone who doesn’t know, is separate from the KM Status Menu available from Editor > Preferences.

A simple AppleScript (found online years ago because I’m no scripter) checks for unread mail. If there’s none, nothing visible displays in the menu bar. Technically, what’s displayed is a Space, otherwise the text Keyboard Maestro will appear.

If there are unread mail messages, I’ve configured a red emoji to display since that color will catch my eye. That portion of the macro could be left blank and the menu bar will instead show the number of unread messages (1, 2, 3, etc.). The visual indicator is what’s important to me, not the count, but know that configuring the macro that way will show the count as standard text (i.e., black or white depending on the machine’s dark mode setting).

KM Version 11.0.4
macOS 10.14.6 Mojave
image

The AppleScript:

tell application "Mail"
	set unreadCount to (get unread count of inbox)
	if unreadCount > 0 then
		return (unread count of inbox as string) & ""
	else
		set finalText to ""
	end if
end tell
set finalText to ""

Unread Mail Menu.kmmacros (4.3 KB)

1 Like

That's a neat and useful macro—I also keep my dock hidden, so seeing it in the menu bar is a nice visual. However, I wanted to see the actual unread count, but still in a way that captured my attention, so I went with a two-character emoji version.

This modified macro displays the message count up to 19 using two emojis, then shows "20+" for counts of 20 or more:

image

Download Macro(s): KM Unread Mail Menu with counts.kmmacros (5.7 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 15.5
  • Keyboard Maestro v11.0.4

I used an array variable to hold the emojis, so it's pretty easy to change as you wish—want to see counts up to 100? Add one more digit, and then enter the emojis for each value up to 100. (It will be easier to do this in TextEdit then copy into Keyboard Maestro.) Also remember to change the value in the case statement from 19 to whatever new upper limit you used.

There aren't many choices for Unicode/Emoji digits, unfortunately, and none are colorful. But the double-width gray Emoji catches my eye well enough. (I also changed the "no new emails" invisible space into an em-dash, as I have the icon in the middle of some others, and seeing something there looks better than seeing a gap.

-rob.

1 Like

I haven't updated the above-linked macro, but I made a slight change: I added a second trigger to the macro:

image

This second trigger forces the count to update when I switch out of Mail, so the menu bar count will be adjusted as soon as I'm done processing my new messages.

-rob.

Alternatively, have your 10-emoji array then substring-replace on each number:

Emojied Counter Demo.kmmacros (4.2 KB)

Image

1 Like

That's slick—thanks!

-rob.

Here's version 2.0, now named simply Open Mail (read on for why)...

Download Macro(s): Open Mail.kmmacros (12 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 15.5
  • Keyboard Maestro v11.0.4

Changes in this version:

  • Using @Nige_S' "emojifier," the macro now displays any new message count you like with only two changes: Change the limit (currently 98) in the early part of the macro, and change what you want to see when over the limit (currently "99+") near the end of the macro.
  • Added the previously-mentioned second trigger, so the macro runs automatically when Mail deactivates. This keeps the menu bar icon in sync with the unread count as soon as you switch from Mail to something else.
  • Added a trigger detector, so that if you click the unread count menu bar icon, you can open Mail directly by picking the Open Mail item that appears—hence the new name for the macro.
  • You can control the number of digits in the icon by changing the "With format" option in the (green) "Set format to two places" Set Variable action.
  • Cleaned everything up a bit, renamed actions for easier follow-along, etc.

-rob.

1 Like

A busy day of work at the computer might mean 10 minutes. Today was probably under 3 minutes. The computer is just not a big part of my job and Keyboard Maestro and Hazel significantly cut down the time my computer work takes. So while I was doing non-computer work this morning I had this same idea:

That was the one aspect which was bothering me about the macro after a couple days of use and it dawned on me this morning to do just that. But credit to griffman: He posted it first so all the glory of that addition belongs to him.

As for the other changes.. Wow. What @Nige_S and @griffman have done is simply beyond anything I could have developed.

I suspect the same eye-catching aspect will be true for me. I say I suspect because your macros don't exactly work on my system. I can confirm your v2 runs, Though no em dashes appear in the menu bar when there are no unread messages, I can click where they should be and I get the Open Mail menu item. And if there are unread messages, no count appears in the menu bar.

I swapped your emoji with Mojave emoji in case that was an issue with my older OS. No effect. So I don't know why it doesn't properly run on my machine, but that's okay. Truly. My original version will be ample, and I've been planning for a couple years to move from this 2013 MacBook Air to an M4 machine sometime this year. Then I can upgrade to the griffman version.

Amazing work from both of you. Thank you.

If you want to dig in, adding a Debugger Start action might help you see what's going on.

-rob.

I've never used the Debugger actions, so don't hesitate to correct anything I do or guide as you see fit. Placing the Debugger Start action at the beginning (or end) of the macro brings just this message:

image

The debugger runs step-by-step; click the "down arrow" icon (second from right) to proceed to the next step. Click again for the next step, etc. Each time, you should see text below the listed action; if you don't, click the topmost 'x' at the right of the box to reveal the variables section.

More on the debugger:

https://wiki.keyboardmaestro.com/manual/Macro_Debugger

-rob.

It walks through your macro steps and displays the same information as is in your macro, "If triggered from the menu, open Mail," "Store the emojis for creating the menu bar display," etc.

I'll note now (and I should have before), that changes properly display in the Editor. For example, if I have no unread messages, I see this at the end of the macro while in the Editor (em dashes):

image

And if there's one unread message (emoji of 0 1),

image

But you don't see anything in the menu bar? That's odd…is the group set to show rg_MailIcon as its icon? Or did you put the macro in your own macro group, by chance?

-rob.

1 Like

That was exactly the issue. Great call.

And I can now say that on this 13" screen the emoji are amply eye-catching for me. So much so that since I deal with few enough emails I won't even need the leading zero for a single-digit unread count.

1 Like

I actually added the zero because I have a 27" display, and wanted the numbers to take up a bit more space in the menu bar :).

-rob.

No surprise with the work you do.I kind of knew your emoji would be okay. For one, I only keep WiFi, clock, and Spotlight visible and everything else hidden with Bartender. But I also have an out-of-development menu bar utility that appears when it periodically crashes; it's a black & white icon about the same size as the emoji you used, and I regularly notice when it appears.

Replacing that utility, Wraparound, with KM when I upgrade to a new Mac will be a challenge.

That is going to be quite the challenge, because of how macOS does screen coordinates—it's relatively easy for a single display, but with multiple displays, I'm honestly not sure how you'd do it. And what's worse is I don't see a way to make it work well without having the macro running as often as you're willing to put up with it—that seems like a ton of CPU to waste on watching the mouse.

As an alternative, you could write a pretty simple macro that just jumps the mouse from its current location to the exact opposite location, and invoke it via a keyboard shortcut. Not as seamless, but tons easier to write :), and doesn't require the macro be constantly running.

-rob.

Yeah, the little bit I've pondered how to do it I've only come up with ideas like you describe. No testing has been done. That's reassuring since you're more adept than I am, so good to know I was thinking logically.

Generalising it to work with any display arrangement would be a real pain. For a known, fixed, arrangement it'll range from easy to "a bit of a faff".

The easiest is two (or more) equal "sized" monitors arranged horizontally, top-aligned:

Wrap Pointer.kmmacros (7.6 KB)

Image

Which, if left running, "costs" about 10% of a CPU core.

2 Likes

As a one-display user (notebook), I expect this to be a thorough faff. Maybe more.

Impressive knowledge here.