How to Tell if a Process Is Busy

I frequently encounter the need to determine whether a process is ready for the next action from KM. I've tried a number of approaches including waiting until a menu item is available and waiting for a found image that doesn't appear until the application has completed whatever it was doing.

But I think that I've found a more general approach. If a process is busy, it won't answer a query from System Events. So the trick is to wait until a System Events tell process <process name> to get window 1 command successfully completes.

I've tried this with several applications and it has worked. If others try it I would be interested in hearing about any cases where it fails.

The subroutine executes the following AppleScript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Keyboard Maestro Engine"
	set theProcess to getvariable "localProcess Name" instance (system attribute "KMINSTANCE")
	set theDelay to getvariable "localRetry Time" instance (system attribute "KMINSTANCE")
end tell

if theDelay is "" then
	set theDelay to 0.5
else
	set theDelay to theDelay as number
end if

tell application "System Events"
	tell process theProcess
		set gotIt to false
		repeat while (not gotIt)
			set gotIt to true
			try
				delay theDelay
				get window 1
			on error
				set gotIt to false
			end try
		end repeat
	end tell
end tell

WaitForProcess.kmmacros (5.0 KB)

3 Likes