How to pause macro until app completes specific task

I'm trying to figure out how to add a conditional pause to my macros based on the application state. I have some macros that will trigger PDFPen Pro to run various tasks on a large quantity of PDFs with each containing a large number of pages (a few hundred pages in many cases). The time to complete the tasks can vary significantly from one PDF to the next so I can't simply add a set pause interval.

The main three tasks in PDFPen are to deskew and rotate the PDF, apply OCR to the PDF, or remove the OCR layer from the PDF. The first two tasks each display a progress window while running, but the window doesn't seem to actually be a separate window that KM can detect. The window doesn't have a title when I display all Windows from the dock for the app while a task is running, I can't switch between that prompt window and the main background window containing the PDF using CMD+`, and when I set KM to check for a keyword displayed in the prompt window it doesn't detect the text in the prompt window. Any suggestions on how to approach this?

Below are examples of the prompts when the different tasks are running (or in the case of removing OCR, the saving prompt appears briefly once the task completes). I also have a screenshot of my pause action that failed to detect Deskew in the prompt when the deskew task was running.

Might have to rely on the image search action and not finding those specific images. None seem to have document-specific info, so it should work…in theory.

Alternatively, you might be able to check for the Cancel button not existing.


Thanks for the suggestions. I'm still working on it, but it seems that the Cancel button isn't detected (at least as a button, I didn't try it as an image), but the found image action is working for the pause until condition.

I have one other problem with this macro. I send CMD+S and CMD+W to the application at the end of the macro to save and close the currently open PDF, but those key inputs are not working for some reason. If I manually go to the open PDF I can use the keyboard shortcuts and they work as expected. Any thoughts on why that would be? Can KM not send the shortcuts to background apps? I attached the macro. I didn't see an option to share my macro to an existing forum topic, so I assume this is how I would share it now.

Deskew and Remove OCR.kmmacros (36.4 KB)

I usually just share it to a new post, cut from there, paste here, then delete the other post.

It should be able to. Does it work if you use the Menu action to select and activate a menu item?


When I use the menu item action it saves and closes as expected, but Type a Keystroke action does not. I guess it is good enough, just seems odd.

Similar to griffman's suggestion to check for the Cancel button not existing, a Pause Until action looking for a menu item to be enabled (or disabled) often works. That is, while tasks are running, certain menu items may be unavailable but become available when the tasks are completed.

That's a good idea, but why would the menu action work when the keystroke action does not? I have them in the same locations in the macro with the same pauses before each to allow for the menu items to be active. Still seems like the menu and keystroke should either both work or both fail, unless the extra second or two that the menu action takes to perform the command is the interval in which the menu item becomes available, but that doesn't seem to be the case in my testing. ¯_(ツ)_/¯

First, instead of your keystroke action it's usually more reliable to use the Select A Menu Item action. That is, instead of using KM to send Command–S, have KM select File > Save.

As for my suggestion, I was putting it forward for your initial query regarding "how to add a conditional pause to my macros based on the application state." A menu item is often disabled (grayed out) when tasks are running, then enabled again when tasks complete. That can be a successful method to determining when an application state has changed.

Admittedly, KM can be impressively fast sometimes, so it may be also be necessary to add a brief, time-based Pause action before initiating the next step of your macro. All told, that's

—have KM detect a change in application state;
—tell KM to pause for a split second;
—tell KM to initiate the next desired action.

Thanks! That is working well and is a really clever way to allow the timing to be precise. I was able to remove all of the interval pauses and just make them conditional pauses based on the menu state. I appreciate your help.