If you're like me, you love Spotlight, but can't imagine what Apple was thinking when they chose to provide Kind and Last Opened as unchangeable default criteria in every new Finder search window. Most of the time, I want to search by file name only, or I get many more hits than I need.
The following AppleScript opens a new Find window and changes the search criteria to Name, and then puts the cursor in the name field. To set it up, launch the AppleScript Utility in /Applications/Applescript, and check both 'Enable GUI Scripting' and 'Show Script Menu in menu bar.' You can set 'Show Library Scripts' to your preference.
Then paste the AppleScript code below into Script Editor, and save it as a new compiled script called "Find By Name." If you want all users on the system to be able to use the script, put it into into /Library/Scripts. If you want it to be available to your user only, put it in the Scripts folder (which you may need to create) in the Library Folder of your home folder. Then, whenever you want to Find By Name, just choose the script from the Script Menu, which is among the icons on the right side of your menu bar.
If you find that the script is choosing Name correctly, but isn't putting the cursor in the name field, try uncommenting the delay 1 line by removing the leading --, and save the script again.
tell application "Finder" to activate
tell application "System Events"
tell process "Finder"
click menu item "Find…" of menu "File" of menu bar 1
tell window 1's splitter group 1's group 1's group 2's group 1
click pop up button 2 of group (count groups)
keystroke ("n" & return)
-- delay 1
try
set value of ((attribute "AXFocused" of text field 1) ¬
of group (count groups)) to 1
end try
end tell
end tell
end tell
If anyone has any suggestions as to the best way to trigger this script with a keyboard shortcut, please post them!
[robg adds: This hint explains how to modify the default Finder search so that it's always set to do a find by name, though that solution is more complex. And this hint explained that using a double-quote as the first character in the Spotlight search will restrict matches to filename (and properties, as in Word docs and ID3 tags) matches only. I like this AppleScript solution for its ease of implementation. As far as activating it, I put the script into a new Automator action and saved it as a Finder plug-in. Now I can do a find by name via the contextual menu, and it will automatically be set to search the selected folder in the Finder. Nice...
Now please, Apple, given the amount of work we're all putting into getting a simple 'find by name' search done, can we please have it back as a default option in 10.4.x?]

