Duplicate paste in Finder "Find" field

I've created a macro that searches a specific Folder for a variable. The problem is, the variable sometimes gets pasted into the "Find" field twice and I do not understand what is causing it (it is sporadic). Any help much appreciated.

Keyboard Maestro 8.0.4 “Search Jewelry Folder” Macro

Search Jewelry Folder.kmmacros (31 KB)

Hey Ray,

If you want this to be pretty bombproof then you’ll want to use AppleScript.

This will retrieve your PIN value from the associated PIN variable in Keyboard Maestro, open the correct window (or bring it to the front if already open), and insert the PIN value in the search field.

-Chris

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/12/21 12:30
# dMod: 2017/12/21 12:44 
# Appl: Finder, System Events
# Task: Open a given Finder Window and input a Search Value in the Search Field.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @System_Events, @Open, @Window, @Input, @Search, @Field, @Value
----------------------------------------------------------------

set targetFolderPath to "/Volumes/SanDisk Blade/Jewelry/" -- Replace with correct path.

tell application "Keyboard Maestro Engine"
   set searchString to getvariable "PIN"
end tell

tell application "System Events"
   open disk item targetFolderPath
   tell application process "Finder"
      tell window "Jewelry"
         tell toolbar 1
            set hitList to first UI element of groups whose subrole is "AXSearchField"
            repeat with i in hitList
               if contents of i ≠ missing value then
                  set searchField to contents of i
               end if
            end repeat
            set value of searchField to searchString
         end tell
      end tell
   end tell
end tell

----------------------------------------------------------------

Thank you for this, Chris. I’ll try to implement it in my macro.

I can see my efforts to get around learning Applescript are failing. I am quite certain most of the macros I’ve created in KM would be much more “bombproof” using Applescript.

Next question: How do I access the results? I expect to see a “Finder” search window. That does not seem to be one of the options. I tried several of the “display” options with no results. Sometimes the expected results is “nothing”. That tells me the file does not exist. How does the Applescript handle that?

Hey Ray,

Hang tight. I'll make the search activate here in a sec.

-Chris

Hey Ray,

Okay — this version will activate the search.

See the new code block starting at line: tell searchField – New code to execute the search. in the code.

-Chris

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/12/21 12:30
# dMod: 2017/12/21 16:12
# Appl: Finder, System Events
# Task: Open a given Finder Window and input a Search Value in the Search Field.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @System_Events, @Open, @Window, @Input, @Search, @Field, @Value
----------------------------------------------------------------

set targetFolderPath to "/Volumes/SanDisk Blade/Jewelry/" -- Replace with correct path.

tell application "Keyboard Maestro Engine"
   set searchString to getvariable "PIN"
end tell

tell application "System Events"
   open disk item targetFolderPath
   tell application process "Finder"
      tell window "Jewelry"
         tell toolbar 1
            set hitList to first UI element of groups whose subrole is "AXSearchField"
            repeat with i in hitList
               if contents of i ≠ missing value then
                  set searchField to contents of i
               end if
            end repeat
            
            tell searchField -- New code to execute the search.
               set value to searchString
               perform action "AXConfirm"
            end tell
            
         end tell
      end tell
   end tell
end tell

----------------------------------------------------------------