Limit Macro to 'Keyboard Maestro User Input' Window

macos 10.14.6
KM ver. 9.2

I would like a macro to run only when the Keyboard Maestro User Input floating window is open. I believe this window belongs to the background process Keyboard Maestro Engine and I don't know how to address it.

Basically, while that window is present and another application is also running, I want to trigger a macro with numpad-asterisk to insert a string of text by typing.

When the user input window isn't present, I want that asterisk key to retain its normal function.

Thanks for your attention.

Floating windows that aren't "seen" the same way as other windows are tricky. One option would be to set a global variable to 1 before each of your prompt actions, then set it to 0 directly after them. Then make a macro triggered by the numpad asterisk with an if/else that performs the typing action if the variable is set to 1. Kinda laborious, but I can't think of a better solution.

There's probably an Applescript way of doing this that will detect if KM has a prompt window running, but it's a bit beyond my current ability to figure that out. @ccstone?

1 Like

Uploading macros is not working today for some reason, so I can't upload the sample I built for you... but here's a screenshot as well as the AppleScript itself that should get you started. I actually put this together for another user sometime last year but saved it since this question comes up from time to time.

The AppleScript checks to see if the user prompt window exists and saves that to a variable.
The if action is designed to run your actions if that variable indicates the prompt exists AND an application is running. Since you didn't specify which app, I just used Finder as an example. If the app is not running and the prompt doesn’t exist, then you can use an insert text by pasting action to input the key you need.

Let me know if you have any questions and I'll be glad to help further.

AppleScript (click to expand/collapse)
----------------------------------------------------------
# Author:				Chris Thomerson
# Version:				1.0
# Version History:		1.0 (Initial script)
#
# Created:				Monday, October 11, 2021
# Modified:				Monday, October 11, 2021
# macOS:				11.6 (Big Sur)
# Tags:					Keyboard Maestro 
#
# 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"
	tell application process "Keyboard Maestro Engine"
		if (first window whose subrole is "AXSystemFloatingWindow" and title is "Keyboard Maestro User Input") exists then
			set promptWindowExists to yes
		else
			set promptWindowExists to no
		end if
	end tell
end tell

### Requires Keyboard Maestro 8.0.3+ ###
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	setvariable "local__uipExists" instance kmInst to promptWindowExists
end tell
Screenshot (click to expand/collapse)

2 Likes

Thank you for the guidance!

Thanks for your generous help. I'll look into this tomorrow and see how I get on with it.

1 Like

Told you there'd be an applescript way! @cdthomer to the rescue!

2 Likes

Just tried @cdthomer's script and it works nicely.

Worth noting that it doesn't seem to work for Prompt With List.

For the Prompt With List try this on for size:

AppleScript (click to expand/collapse)
----------------------------------------------------------
# Author:			Chris Thomerson (@cdthomer)
#
# Current Version:	1.0 (Initial script)
# Version History:	1.0 (Initial script)
#
# Created:			Saturday, March 26, 2022
# Modified:			Saturday, March 26, 2022
# macOS:			12.3 (Monterey)
# Tags:				Keyboard Maestro
#
# 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"
	tell application process "Keyboard Maestro Engine"
		
		(*
			repeat until window "Prompt With List" exists
				delay 0.1
			end repeat
		*)
		
		if window "Prompt With List" exists then
			set pwlExists to "yes"
		else
			set pwlExists to "no"
		end if
	end tell
end tell

pwlExists

It would no doubt need to be adjusted for any end-users macro but it gives you the idea. The commented out portion was just part of my testing and can be deleted.

2 Likes

Yep! Brilliant. Thanks Chris!

You're welcome. I seem to remember you and I talking about UI Browser in the past, it makes it really easy to identify "windows" that aren't actually windows.

Funnily enough I'd tried that and wrote a script to output the name of the front window, but I didn't tell the Engine, so it was returning the name of the app window behind. You learn something new every day!

I just tested entering text by typing into a list prompt and oddly it ignores it when you hit enter, unlike manually typing it. I wonder why that could be. @JingleDjango, is it working for you?

Unfortunately I'm getting no result using the script above. KM intercepts the hot key (doesn't type '*' into the text field) but neither IF nor ELSE conditions execute. Have I done anything obviously wrong?

Stink, I made a rookie mistake... wrap those yes and no bits in quotation marks like so:

			set promptWindowExists to "yes"
		else
			set promptWindowExists to "no"

Then try it again and report back.
I don't know why NEITHER actions would execute (I don't see any obvious issues)... but let's start there at least.

1 Like

Bingo!

At first glance, this performs exactly as hoped. This will save me some repetitive strain naming clips for a game audio project.

Thank you again for taking the time to help me out with this.

1 Like

Awesome! Glad you were able to get it going and it will help your workflow. Also glad to have helped and don't hesitate to reach out if you need more help!

-Chris