Is there any way for KM to know that Alfred is now the focused window?

This thread has been very helpful to me. I have a question though.

I am using the suggestion here to create a workflow in Mail. I have grown used to MailMate, which lets me use single-key shortcut. For example, while in a message list, I can type "C" to compose a new message, "R" to reply to the selected message, etc.

I can create this in Mail using the above example, and setting the Macro Group to "When a focused window title contains" (the opposite of the above, because I want the single-keys to work while I am in the message window, but NOT to work when I am in a compose window because I need those single-key shortcuts to be regular keystrokes, "c" "r" etc so I can type a message). I hope this makes sense.

This all works well. When I am in the message window I have my single-key shortcuts. When I type "c" I get a compose window, and the Macro group deactivates.

Except ... when I use Alfred while in Mail, I lose these keystrokes "c" etc.) because KBM thinks Mail is still the focused window. Even though Alfred's window pops up over Mail, and I can type into Alfred, apparently Mail is still the focused window and I lose those keystrokes.

My question is: is there any way for KBM to know that Alfred is now the focused window so that the Macro Group is no longer activated?

Again, I hope my explanation makes sense.

1 Like

I moved your post to a new topic since it is really different from the current topic.
This will help you get better attention, and future reader to find your Q&A.

@peternlewis can confirm, but I'm pretty sure KM can't "see" windows like Alfred that are not standard macOS app windows.

1 Like

I have a similar need. In an app, I use the arrow key to simulate tab switch, which needs a combination of keys.

I make the Alfred icon visible, and use Found image in If condition to make it work.
If the Alfred icon is present, it means I'm working with Alfred, it will be used as regular arrow keys. If not, it will switch tabs in that app.

image

1 Like

What are you using to trigger this?

I use the Right Arrow key as trigger.
This is for next Tab.

Just realized a problem: after I type the right arrow key, Alfred proceeds for further action, and the Alfred icon will be gone. Then the macro will not work as expected. So, it does not work well for arrow keys. But I rarely need to use arrow keys for further Alfred actions. So it is still useful to me.

You can do something like this (change "Simulate keystroke 'G'" to whatever action you want):
image

As I just said, once the Alfred icon is gone, the macro will not work as intended. You need to be aware of that.

And I just realized I have a problem too. After typing "c" (which types Cmd-N for a new message), I get a compose window in Mail. But even though it seems like this should be the active/focused window, with no title, the Macro group is still active (even though the message list window with the title I use for "Available when a focused window title matches" is in the background, not the focused window). I'm puzzled.

This may not fully fix your issue, but maybe you could try setting the MG to EXCLUDE Mail. If Mail is frontmost when you do this, nothing should happen.

I forgot to mention, I also have a macro to activate/deactivate the macro group.
If I'm working on Alfred, and the macros are interfering, I will temporarily deactivate the macro group for that app. After finishing the operation with Alfred and back to the app, I will then activate the macro group again.

When it is deactivated, it is essentially the same effect as the app being excluded by Keyboard Maestro, as @JMichaelTX said above. In my case, I have some macros to work with the app. Therefore, I can't just exclude the app in Keyboard Maestro. The activate/deactivate action works well for me.

1 Like

I've also struggled with this. I tried disabling the macro group on Alfred activate but that did not work. I'm currently just using a shortcut to enable/disable the macro group with a hotkey.

It would be SOO wonderful if these productivity tools could be made to play nice. I think many people will be users of both. The key is some kind of trigger for KM that recognizes when the Alfred window is active and deactivated, we could easily then enable/disable any macro groups that were interfering.

Hi @narath,

I've found a way to get around this issue. Take a look at the post here

Hope it helps!

Thanks @Martin - so clever and helpful. I've done as you suggest and it works beautifully (disabling the macros when Alfred launches).

The enabling the macros again does not work however.
I added another macro to re-enable them when Mail activates again (but launching and returning from the Alfred window does not trigger an activate event), so it does not work well when activating Alfred from Mail and then returning to it.

That was an idea. I have made some changes to show how it could be implemented.

(Edit: requires KM 9.0+ for Cancel a Specific Macro, see actions:Redirect Control Flow [Keyboard Maestro Wiki])

Aflred - Detect if Active.kmmacros (9.7 KB)

Explanation:

  • When you activate Alfred, the variable local__AlfredActive will be true. It will then disable the macro groups you set up there.
    • It then listens to your keyboard press. There are two ways we may interact with Alfred. One is to press return to execute a command, the other is to press esc to dismiss the Aflred float window. When either of them happens, then KM will re-enable the disabled macro groups.
  • A third unusually way to dismiss the Alfred float window is to use the same hot key that we use to bring up the Alfred float window. In my case, it is ⌃Space. In this case, the macro will be trigger again, the pause until key press action of the previous instance is still running. But we would like KM to stop listen to Esc or Return once the Alfred window is dismissed.

To solve this issue, we have to implement something else, that is, to add cancel a specific macro action, and we use a variable called DND__alfred_active to tell whether the Alfred window is active. if so, dismiss the Alfred window and cancel the previous macro which is still listening to keypress.