Automate the Clearing of Text in the Keyboard Maestro Search Field?

I'm working on a macro update and within the sequence of actions I want to safely and reliably clear the Keyboard Maestro Search field.

Is there a native method that I'm missing to do that? If so, then the remainder of this post is moot. But if not, this seemingly simple task becomes more challenging than one might think...

At first blush, it seems simple:

  • Select a Menu Item action: Edit > Find > Find..... When this action runs, if the Search field includes any text, it is automatically selected. Thus an Edit > Select All is not required.

  • Type a Keystroke Delete

But during a side conversation with @ronald, he expressed understandable concern about automating the Delete key. (What if the automation somehow failed and the Delete deleted some macros or macro groups?)

My first thought was to simply replace Delete with Space as that would be much less likely to cause a bad side effect. But when testing that, we discovered that the Keyboard Maestro Editor automatically enters all: if a Space is entered.

So after lots of testing, here's a sequence that seems to work safely and reliably. I've included some embedded comments with more information.

Download: Safely Clear the 𝗦𝗲𝗮𝗿𝗰𝗵 Field.kmmacros (13 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 15.3 (24D60)
  • Keyboard Maestro v11.0.3

If I've missed something simple, please don't make fun of me! :grinning: But if you have some related experience/insight, please do comment. TIA!

1 Like

Frankly, I don't like the delete in the macro, although everything appears logical.

Hi, @ronald. In the post above that you deleted, you suggested the clicking the X on the far right side of the Search field. That might be reliable if:

  • Keyboard Maestro is activated immediately beforehand.
  • The coordinates are based off the upper-right corner of the window.

Here are the actions...

Download: Safely Clear the 𝗦𝗲𝗮𝗿𝗰𝗵 Field (method 2).kmmacros (3.5 KB)

Macro-Image



Note that Restore Mouse Location is an option for the Move or Click Mouse action.


Initial testing looks promising.

1 Like

2 options - click at found image and move and click using the KM mouse display to get the coordinates using the KM editor as a reference.

I tried AppleScript using chatGPT and UI Browser but neither worked.

See my post above. On my system, this seems to work if the coordinates are relative to the upper-right corner of the Keyboard Maestro Editor window.

Hello Jim,
method 2 works perfectly for me
Would that not be screen size and resolution dependant ?
I am just asking. I am confused

Resolution is not a problem, but Keyboard Maestro Editor window size is an issue if you are using the upper-left corner.


If using the upper-right corner, the position of the X seems to remain fixed regardless of the Keyboard Maestro Editor window size. Knowing @peternlewis, this was intentional and not merely fortuitous.


So far I think this is the winning approach.

1 Like

yes, I agree

Not native -- but how about:

tell application "System Events"
	tell process "Keyboard Maestro"
		tell window 1
			tell text field 1
				set value to ""
			end tell
		end tell
	end tell
end tell

The only obvious (to me, and remember that I seldom think things through properly :wink: ) problem is if the Action pane is open -- the "main" Editor window is then window 2. Is that an issue?

1 Like

Hi, @Nige_S. Thanks for weighing in!

Based on your suggestion, I had high hopes for this...

Keyboard Maestro Export

And in most cases that approach works as expected. Unfortunately, however, if the cursor is already in the Search field and it does not contain any text, when the AppleScript runs, the Keyboard Maestro Editor does this...


Odd, right?

My instinct would have been "escape". :thinking:

Which suggests the solution -- check for text before blanking the field:

tell application "System Events"
	tell process "Keyboard Maestro"
		tell window 1
			tell text field 1
				if value is not "" then set value to ""
			end tell
		end tell
	end tell
end tell

For the "is the action pane open?", it will slightly quicker to check in the AS then with macro actions. IIRC, last time this cropped up we did something like:

		if name of window 1 is "New Action" then
			set winIndex to 2
		else
			set winIndex to 1
		end if
		set flag to false
		tell window winIndex
			...

But if you interact with the Search box, via the UI or script, the "Actions" pane closes. You probably want that -- if you don't you'll need to re-open it after the box has been cleared. Putting that all together:

tell application "System Events"
	tell process "Keyboard Maestro"
		if name of window 1 is "New Action" then
			set winIndex to 2
		else
			set winIndex to 1
		end if
		set flag to false
		tell window winIndex
			tell text field 1
				if value is not "" then
					set value to ""
					set flag to true
				end if
			end tell
		end tell
		-- Do you want to reopen the Actions pane?
		-- If so, uncomment the following section
		(*
		if winIndex = 2 and flag then
			repeat while name of window 1 is "New Action"
				delay 0.1
			end repeat
			delay 0.1
			keystroke "k" using command down
		end if
		*)
	end tell
end tell
1 Like

Hi, @Nige_S. This works great! Thank you!

I tested it with many different starting scenarios. During the testing, one time (and not repeatable) the following error occurred:

Action 14963480 failed: Execute an AppleScript failed with script error: text-script:807:821: execution error: System Events got an error: osascript is not allowed assistive access. (-25211)

To address the possibility of similar random errors, I've enclosed your AppleScript in an Until action.

Thanks again.


Download: Safely Clear the 𝗦𝗲𝗮𝗿𝗰𝗵 Field (method 4).kmmacros (8.9 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 15.3 (24D60)
  • Keyboard Maestro v11.0.3

Unless there's a hidden native method (@peternlewis?), I think we have a winner. :1st_place_medal:

1 Like