Test for "Prompt for User Input"

I have a macro that does Prompt for User Input. Before displaying the prompt, I want the macro to check if the prompt is already being displayed (so that it doesn't display a second prompt). I've tried setting a variable to 1 when I display the prompt and setting it back to 0 when I dismiss the prompt, but this isn't reliable because I may reboot between setting the variable to 1 and resetting it to 0. Is there a way to check to see if Prompt for User Input is currently showing?

Hey there! This can be done with AppleScript. I've included the script itself as well as a macro that has the script embedded in it with some comments that explain how to use it.

Check it out, see if it works for you, and let me know if you have any questions!

-Chris

Check for User Prompt Window existence.kmmacros (12 KB)

Macro screenshot (click to expand/collapse)

----------------------------------------------------------
# Author:				Chris Thomerson (@cdthomer)
#
# Current Version:		1.0 (Initial script)
# Version History:		1.0 (Initial script)
#
# Created:				Friday, January 14, 2022
# Modified:				Friday, January 14, 2022
# macOS:				12.1 (Monterey)
# Tags:					Keyboard Maestro, User Prompt
#
# DISCLAIMER
# Permission to use, copy, modify, and/or distribute this
# software for any purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
----------------------------------------------------------


tell application "System Events" to tell application process "Keyboard Maestro Engine"
	if window "Keyboard Maestro User Input" exists then
		set uipExists to "true"
	else
		set uipExists to "false"
	end if
end tell

Thank you, Chris. Are you sure that I don't have to add

return uipExists

to the Applescript?

1 Like

Technically, no, although generally I will go ahead and that at the end of scripts just as good practice. It's especially important if you need multiple values returned, in which case I would add this at the end:

{value1, value2, value3} ...and so forth...

...but since this script is so basic, it's the only information/value that is generated. I just confirmed that adding this action to the macro as is does indeed show you if the UIP exists or not, even though the AppleScript does not explicitly call for that information at the end.

Hey Barry,

I'm not sure if this is applicable to your problem, but you can use a Semaphore Lock action to prevent a macro from running a second instance if it's still running.

-Chris

My approach is to simply do a Found Image test for the prompt. To be sure you're finding the exact prompt you're looking for, you might name those response buttons distinctively.

Thanks for this macro @cdthomer. I needed it today :slight_smile:

Wonder why semaphore lock does not work for macro that has Prompt for User Input action. Is it a bug?
test

Happy to help!

It appears to be working as designed... but likely your timeout is set to the default of 99 hours... which means when you dismiss the first prompt(s), the next execution proceeds since the timeout hasn’t been met. Try adjusting it to 0.01 seconds and it should prevent subsequent prompts from appearing. Side note: you technically don’t need the unlock action either... once the macro finishes executing the semaphore is unlocked automatically. :wink:

2 Likes