Avoid Attempts to Prematurely Interact with an Application

Suppose you want to open an application, wait until it opens, and then somehow interact with it once it opens. This sounds simple, right?

As simple as it sounds, this gets more complicated when:

  • The app opens somewhat slowly, i.e., requires more than a few seconds.

  • You don't know the name of the app until the macro run-time.

  • The app is already running in another Desktop Space.

  • The app is the Finder.

  • The app is non-standard, e.g., Obsidian, and does not include an About menu item—which is commonly used to check that an app is ready.

The actions in this macro account for these circumstances. If you sometimes have timing issues when attempting to interact with an application, you might find this macro helpful.


DOWNLOAD Macro File:
Activate an App and Wait Until It Is Ready.kmmacros (60 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image


If you find an macOS application that does not work with this macro logic, please enable the debug actions and post the generated screenshot here. For example, if Obsidian was not included in local_Missing About Menu Item, the following would appear:

Debug Output-image

2022-03-07 17.41.45

Your code handles many cases, but I'd like to share the method that I use, which isn't as flexible as your method: (but it does handle apps which have delays for closing and opening)

I do this sort of thing a lot, and I also have a counter for each time that I do this. If the counter reaches 3, I usually abort my macro (and I send myself a text message that my macro failed.) Even with your method, adding a counter could be a helpful mechanism to stop when it can't continue.

1 Like

If you have Keyboard Maestro v10+, the logic I provided above can be implemented with a Subroutine and calling macro.


DOWNLOAD Macro Files:
caller + sub—Activate an App and Wait Until It Is Ready.kmmacros (63.7 KB)
Note: This macros were uploaded in a DISABLED state. They must be ENABLED before they can be run. If they do not trigger, the macro group might also need to be ENABLED.


Here's an image of the calling macro.

Macro-image


Aside of the built-in methods to pass and return values, Subroutines in Keyboard Maestro v10+ provide a great feature which allows one to easily see the Callers in a pop-up menu.



If you don't have Keyboard Maestro v10+, you could still run the main logic using a calling macro that includes the Execute a Macro action (replacing the Execute a Subroutine action), but the main logic would need to be modified to use the %TriggerValue%. Also the main logic would need to be modified to return the result (OK|Error) via a global variable or dictionary key (IMO, the latter is preferred) and the caller would need to be modified to read the global variable or dictionary key.