Activate a Menu Item Action Only *IF* a Specific Application Is in Front

I want to suggest an option that I think would be very useful for Keyboard Maestro and drastically simplify the common "keyboard shortcuts" use case.

I realize that this behavior is likely already possible using complex nested rules and boolean logic. But an interface tweak would make this much easier to use.

The "Select or Show a Menu Item" action currently gives two options:

  • Front Application
  • Specific Application

This Specific Application will activate the menu item whether or not the application is currently active / in front.

That behavior makes it impossible to set a single Hot Key Trigger, and then perform per-application actions based on whichever application is in front.

For example, I want to set a "Move Tab Left" keyboard shortcut. This shortcut will perform differently-titled menu actions depending on application.

  • Chrome: "Select Previous Tab"
  • Safari: "Show Previous Tab"

But when I set up Keyboard Maestro as seen in the screenshot, this activates all of the actions. Regardless of which app is active, both Chrome and Safari run that action.

The Keyboard Maestro interface should give a checkbox option to only activate that specific application's action if that application is in Front:

"Select Menu in [Safari] ... Only if in Front [true]"

This will allow for a one-to-many Hot Key Trigger -> Application setup, simplifying the manual entry of so many triggers.

A similar option would be to use the "Front Application" target, but then provide a cascading list of Menu Items to try. Perform the first menu item that is available for the front application, then exit the macro. This will ensure that the first applicable menu item action is run.

1 Like

I can't speak as to why KM doesn't currently offer an option like the one you suggest; I'll let Peter comment on that if he wishes to, though I suspect he has very good reasons for why KM is designed the way it is. That said, I don't think the type of macro you describe is overly difficult to create thanks to app-specific Macro Groups and the Switch or Case action. Here's an example of the latter that does exactly what you want, i.e. perform differently-titled menu actions depending on application:

Tab Left.kmmacros (3.1 KB)
image

2 Likes

@pkamb, @gglick hit the nail on the head. I use both of those techniques in many macros, and both are easy to setup and write.

1 Like

The solution from gglick has worked to solve a big problem for me. I have a Safari KM script assigned to F5, and if I inadvertently hit F5 while in another program, the Mac would switch over to whatever page I had up in Safari and run the command -- not my intention at all. The gglick solution would just render F5 mute in all other applications -- and it gives me the opportunity to use F5 for something else when in a different program.

1 Like

Hey Andre,

In cases where you want to limit a macro's scope to a specific Application like Safari the best practice is to create an app-specific macro group.

Safari -- Test -- Notification.kmmacros (1.9 KB)

You can also add more applications to such a macro group's scope.

Gabe's method has a global scope – and that can sometimes be the better tool for job – depending upon what your needs are.

-Chris

Yes, this is exactly the usability issue/suggestion I was bringing up in the OP.

I think there's a major implicit mental model of "only select the menu item IF the application is active." That's how it works in System Preferences shortcuts and on the Mac in general. Controlling an application that's in the background is what's non-standard.

You should be able to do both, command the app in the background OR only if in the foreground. And you can, via the strategies discussed. But it seems like it would be very useful as a first-class checkbox option on the "Select Menu" action.

I appreciate all the terrific advice here. I don't try to pose as any kind of expert around here. It has taken me a long time to get off Square 1 with KM. But I am only at Square 2, and all I know is what works for me. I suppose there are a lot of KM users just like me.

Have this been solved?
Ie; "Only select the menu item IF the application is the frontmost."

Example:
I have created a Safari shortcut that goes to the next tab. That shortcut happens to be the same shortcut in Mail for indenting the text. Hence, when I'm in Mail and press this shortcut, Safari opens and go to the next tab.
Annoying.

Not enough info to answer completely. If you've a particular problem, post the macro.

But in general -- if you want the entire macro to only run when Safari is at the front, put it in a Group that is "Available in these applications" and add Safari. If you want it to be available all the time except when Mail is at the front, put it in a Group that is "Available except in these applications" and add Mail. You can narrow it down even further and make it only active when certain windows are frontmost.

You can also do this by making the Group available everywhere but enclosing the entire macro in an "If/Then/Else" block -- use "Application condition" then pick "Safari" and "is at the front" then put the rest of your macro in the "execute the following actions" block -- that'll make them only happen when Safari is frontmost. If you want them to happen any time Mail isn't frontmost, set the conditions to "Mail" and "is not at the front".

You can use the same "If/Then/Else" action to control flow for a subsection of your macro, so you could do a bunch of generic stuff, break out in different directions depending on whether Safari or Mail was frontmost, then come back in to do more generic stuff.

All the above are great ways to have the same trigger do different things depending on current state, so you can eg always have ⌃V paste "currently selected thing" into Notes but make the copy op and any pre-processing app dependent.

2 Likes