Ignore Hotkey if Spotlight Search Active

I'm using KM 9.2 on MacOS 11.5.1.

I'm trying to recreate Keyboard Shortcuts used in Gmail so they work in MacOS Mail. Gmail does not require modifier keys to perform actions.

For example, in gmail, if you have KS turned on, and hit the e key, it archives the email.

Apple Mail expects ⌃⌘A

Thanks to ccstone's List Windows macro, I created a group that requires the window title to contain "All Inboxes"

Then I created a few macros, here is "Archive email":

This seems to work, however, if I have Mail highlighted and then bring up spotlight search using ⌘-Spacebar and start typing any of my replacement keys, they get caught by keyboard maestro because it seems to think Mail is still the active window.

  1. How do I get KM to ignore keystrokes entered into spotlight search? (Is this a rule to add to my macro group?

  2. For some reason, my Archive Mail macro seems to require "This string is typed" whereas the rest of my gmail -> apple mail macros work with "This hot key". For example my previous simply maps p to up arrow

Is it obvious why my Archive key only seems to work with "This string is typed" and not a normal hotkey?

  1. I searched through the Macro Library and did not see an existing library focused on this "transition from gmail to Apple Mail" problem. Has anyone else made this transition / created a library to do this that is already better / more complete?

I'm looking to move my email out of gmail and gsuite accounts into the coming Apple mail to get my communications away from Google's all seeing eye.

Mail Modifiers Macros.kmmacros (9.2 KB)

You can't.

Hide Mail H and then run Spotlight.

It shouldn't.


Ah bummer.

I’m guessing you know all the angles on this but just to tap on some possibilities:

There is no way to detect Spotlight search is activated? Maybe MacOs hides this info?

Is this a well known limitation of KM? Not trying to be snarky here, it just jumped out at me right away because I use spotlight a fair amount. (Mostly to launch a handful of common apps I should look to use KM hotkeys instead)

Are there other ways to devise the key mapping I’m after that might get me to the solution?

For example, interrupt command-space to check if Mail is active window, if so hide it and the show spotlight search?

Keyboard Maestro can't directly, but it can be done with AppleScript UI-Scripting.

See the Execute an AppleScript action.

tell application "System Events"
   tell application process "Spotlight"
      if (get name of windows) contains "Spotlight" then
         return true
         return false
      end if
   end tell
end tell

But this gets incredibly cumbersome.

When I use single keystroke HotKeys I use a palette to display them and remind me they're active.

I can toggle this on/off at need.

Keep in mind that you're gaming the (macOS) system, and that always comes with limitations.



It can also be done by examining the output of the ps command. If the cpu column>0, that means the window is active. This can be put into a KM shell script action and tested.

This worked!

Some catches:

  1. There is a small amount lag compared to not doing these checks. (Perhaps this is what you meant by cumbersome?) I have a pretty fast mini, and I can deal with the lag, but curious if there are any obvious ways to make the macro more efficient?

  2. This is perhaps more of a Mail question, however, In threaded conversations, if a given email in a thread is selected as indicated with a blue highlight (shown below) there does not seem to be a way to determine this state.

In this state, an Option-arrow key is used to move between emails. So if this state can be determined, I'd want my keys to use the option modifier. Any ideas on how to figure this out?

  1. Related, and still more Mail oriented, I do not see an obvious way to return focus to the main inboxes pane of the window. Normally, I'd expect ESC to do this. Not sure why Mail doesn't support this, any ideas on how to use KM to do this?

Thanks for this, did you mean top ? Would this be more performant than the AS suggested by cm?

Mail Modifiers Macros.kmmacros (26.6 KB)

It's amazing that it could detect the Spotlight search window.

How about "Alfred"?
I changed Spotlight into Alfred and it does not work.

Edit: Turns out I made a mistake. There were two places to change and I changed only one place. It's working great now!

From this post, an answer provided by Chris as well.

Code for Alfred:

tell application "System Events"
	tell application process "Alfred"
		if (get name of windows) contains "Alfred" then
			return true
			return false
		end if
	end tell
end tell

I found a mistake in my archive macro and that was the only lagging one. I think it is pretty good now.

I did find that the Help menu search input still captures these unmodified keys. Is there a way to detect for Help -> search input active so I can allow the character to be typed the same as the spotlight?

The reason I would go to this trouble is that I think many people will migrate away from gmail and gsuite when Apple debuts their new mail hosting service next year. This is my goal, to host my mail using my domain with Apple instead of Google.

It would be nice to make this as clean as possible so I can share it in the macros forum.

edit: I had planned to have N and P do next and previous and archive, but that doesn't make as much sense in Mail as it did in gmail.

If you use the "inbox zero" goal of archiving "complete" email, the default Mail behavior of "search based on unmodified key entry" is unwanted.

As a result, I have re-enabled N and P to do next and previous, which duplicate the behavior of J and K which are the actual default next and previous gmail hotkeys. (mirroring vim)

Current macro group attached.
Mail Modifiers Macros.kmmacros (26.7 KB)

A follow up question:
Is it possible to use this as a condition for a macro group?

Something like "Available" when "Alfred Search Window is activated".


Alfred is a background-only app, so it is never seen as the frontmost app by the system regardless of whether its UI is frontmost or not.

I don't think it sends a notification event when its UI is activated, so apps like Keyboard Maestro can't notice.

LaunchBar is similar, and I was furious when it became background-only – because it limited my ability to use Typinator, FastScripts, and Keyboard Maestro with it.


Found a way to suit my need.

+Space was my original Alfred Hotkey.

I changed it to ++Space in Alfred, and then let KM to trigger it (see macro below).

The purpose of this change is to keep the original Alfred trigger so that I don't have to adapt to new trigger.

Then I can ask KM to disable some macros or macro groups.
The only thing is that I need to manually enable them afterwards. That's not a problem, because I've been using a hotkey to toggle enable/disable before.

It saves me from the first three unnecessary steps: 1. typing search strings in Alfred interfered by the keyboard mapping macros (set in KM) → 2. disable the macros/macro groups → 3. back to Alfred to type → 4. enable the disabled macros/macro groups.

I don't even need the detection.

1 Like

A second thought:
If someone tends to use the Alfred trigger to close the Alfred search window, then the Alfred running detection might still be useful, because if the hotkey is used to close the search window, then there is no reason to disable those macros/macro groups. We might even want to enable them.

There isn't one.

But there's a kludgey workaround – check for the number of selected messages. This will work only if the selected item is the root level of the thread – and it won't bump you off the end of the thread.

tell application "Mail"
   tell (some message viewer whose index is 1)
      set numberOfSelectedMessages to length of (get selected messages)
   end tell
end tell

return numberOfSelectedMessages

I have J bound to an AppleScript that moves focus into the message list and then activates the Down-Arrow.

So I can move things along if I like.


Thank you for that, I will give it a try.

I'm curious, have you also been trying to tune Mail's shortcuts to behave more like gmail? Or are these ideas just in response to my questions?


I've helped people who wanted to do this before, and of course some is entirely off-the-cuff.

I don't like Gmail.

Then again I'm not very fond of Apple Mail.

The MailHub and Mail Act-On plug-ins (commercial) plus 72 AppleScript run via FastScripts and 32 Keyboard Maestro macros make it bearable.

Apple doesn’t really understand how to make a good mail client, and they've done little to improve Mail in years.

There's not even a built-in means to emplace a horizontal line – or many other features that have been available in other clients for decades now.


I still haven't found a client I like significantly better than my highly modified version of Mail.

Unfortunately it looks like MailHub will die-out once I move away from Mojave.

I miss Eudora...


I meant the ps command. Here it is:

ps -e -o %cpu,command | grep Spotlight.app

If the first number is 0, then Spotlight is not the front window. I tested it a dozen times and it was 100% reliable. There's a 50% chance it's reliable 100% of the time with further testing. I can't speak to the accuracy of the AppleScript since I'm daft at AppleScript but deft at shell. But the AppleScript code looks like it could be 100% accurate in this case.

Usually AppleScript has the most reliable solutions. If alternate solutions are ever banned on this forum, I'll be a lot more quiet. But sometimes it's fun to see an alternate solution. I was even tempted to show you a solution with Find Image, but I knew there would be better solutions than that.