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

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.

1 Like