Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tkbird

macrumors newbie
Original poster
Jan 8, 2024
5
0
Hello,

When I'm searching in Finder, I nearly always prefer to search for file names, so I use "name:". Is there an easy way to use Automator to auto-populate the search box with this when I go to search, so that I don't have to type it in every time? If it simplifies things, the automation could be restricted to when I use Command-F.

Thanks!
 

bogdanw

macrumors 603
Mar 10, 2009
6,118
3,029
I'm not sure if this is what you are looking for, but you can save a search.
“You can save a search to use again. Just click Save below the search field. Your search is saved as a Smart Folder. To be able to quickly access your search in the Finder sidebar, make sure Add To Sidebar is selected.”
https://support.apple.com/guide/mac-help/mh15155/mac
Video - Creating Convenient Saved Searches For Your Mac Finder Sidebar
https://macmost.com/creating-convenient-saved-searches-for-your-mac-finder-sidebar.html
 

tkbird

macrumors newbie
Original poster
Jan 8, 2024
5
0
Hi @bogdanw, thanks for the suggestion! Unfortunately, that's not quite what I was going for. However, I did manage to figure this out. I used the AppleScript below in Automator and a keyboard shortcut for Finder in App Shortcuts. For the AppleScript, I identified the correct element in Finder by using Watch Me Do and dragging that into the AppleScript.

AppleScript:
set uiScript to "click UI Element 1 of group 3 of tool bar 1 of window 1 of application process \"Finder\""
my doWithTimeout(uiScript)

on doWithTimeout(uiScript)
    set endDate to (current date)
    repeat
        try
            run script "tell application \"System Events\"
                " & uiScript & "
                end tell"
            exit repeat
            on error errorMessage
                if ((current date) > endDate) then
                    error "Can not " & uiScript
                end if
        end try
    end repeat
end doWithTimeout

delay 0.15

tell application "System Events" to keystroke "name:"

Search Filename.png

Shortcut.png
 

bogdanw

macrumors 603
Mar 10, 2009
6,118
3,029
I think you can reduce that AppleScript to just:
AppleScript:
tell application "System Events" to keystroke "f" using command down
delay 0.15
tell application "System Events" to keystroke "name:"
 

tkbird

macrumors newbie
Original poster
Jan 8, 2024
5
0
Hi @bogdanw, that's a great alternative! I opted for the button click version because I wanted to avoid having the search criterion row appear. I rarely use that, so I prefer to have more results visible instead. That said, if you can simplify my button click script, I'd definitely be interested!
Thanks!
 

tkbird

macrumors newbie
Original poster
Jan 8, 2024
5
0
Try
AppleScript:
tell application "System Events" to keystroke "f" using {command down, option down}
delay 0.15
tell application "System Events" to keystroke "name:"

"Option-Command-F: Go to the search field." https://support.apple.com/HT201236
Awesome, that's a nice improvement!

Do you know of any way to include a flexible delay that waits for the search field to be selected before typing "name:"? My current delay is from trial and error and may not work if my computer is running slowly. Incidentally, I found I need a longer delay with this new version of the script (0.25).

Thanks!
 

bogdanw

macrumors 603
Mar 10, 2009
6,118
3,029
I don’t know how to make it wait, but here are some suggestions that might speed it up:
- select Workflow receives – no input, instead of Automatic
- try to include telling Finder to activate
AppleScript:
tell application "Finder" to activate
tell application "System Events" to keystroke "f" using {command down, option down}
tell application "System Events" to keystroke "name:"
or
AppleScript:
tell application "Finder" to activate
tell application "System Events" to keystroke "f" using {command down, option down}
tell application "Finder" to activate
tell application "System Events" to keystroke "name:"

- settle for delay 0.3 :)
AppleScript:
tell application "Finder" to activate
tell application "System Events" to keystroke "f" using {command down, option down}
delay 0.3
tell application "System Events" to keystroke "name:"
 

tkbird

macrumors newbie
Original poster
Jan 8, 2024
5
0
I don’t know how to make it wait, but here are some suggestions that might speed it up:
- select Workflow receives – no input, instead of Automatic
- try to include telling Finder to activate
AppleScript:
tell application "Finder" to activate
tell application "System Events" to keystroke "f" using {command down, option down}
tell application "System Events" to keystroke "name:"
or
AppleScript:
tell application "Finder" to activate
tell application "System Events" to keystroke "f" using {command down, option down}
tell application "Finder" to activate
tell application "System Events" to keystroke "name:"

- settle for delay 0.3 :)
AppleScript:
tell application "Finder" to activate
tell application "System Events" to keystroke "f" using {command down, option down}
delay 0.3
tell application "System Events" to keystroke "name:"

Thanks for the ideas! It seems like only the delay works. Oh well, I'm still happy with this end result!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.