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.
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.
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.
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.
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 ) problem is if the Action pane is open -- the "main" Editor window is then window 2. Is that an issue?
Based on your suggestion, I had high hopes for this...
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...
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
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.