Find in Folder

This is an example script using a recent tip in the Applescript FAQ: How can I search for specific items in a folder?

It can run from the Script Menu, as a BigCat Script contextual menu Files item, as an iKey shortcut or saved as a droplet for use in the sidebar.

After selecting a folder (or alias to a folder) in the Finder, running the script prompts for a search string with two button options. Find items that contain the search string in the selected folder or find items in the selected folder and it’s nested folders.

Items matching the search string are revealed (selected and highlighted) in the folder. You can then scroll through the folder to locate the matching items or drag the entire selection to another folder.

OS version: OS X

-- Find in Folder 110804 bc 
-- for use in Script menu, BigCat CM, iKey or a droplet (in the sidebar)

property find_last : ""

on main(s)
	
	tell application "Finder"
		activate
		try
			if s is "" then set s to selection -- on run or on open caller
			set fold_path to s as text
			if fold_path is "" then error
			if fold_path does not end with ":" then -- try alias
				set t to the original item of (fold_path as alias) -- error if not alias
				if class of t is not folder then error -- original not folder
			end if
		on error
			beep
			return
		end try
		
		set reply to display dialog "Enter a search string:" default answer find_last buttons {"Cancel", "Find in Nested", "Find"} default button 3 with icon 1 giving up after 60
		
		if (button returned of reply) is "Cancel" or (gave up of reply) then return
		set find_string to (text returned of reply)
		if find_string is "" then
			beep
			return
		end if
		set find_last to find_string
		
		if (button returned of reply) is "Find" then
			set matches to (items of (fold_path as alias) whose name contains find_string)
		else
			set matches to (items of entire contents of (fold_path as alias) whose name contains find_string)
		end if
		
		if matches is not {} then
			set reveal_list to {}
			repeat with this_item in matches
				copy (this_item as text) to end of reveal_list
			end repeat
			reveal reveal_list
		else
			beep -- no matches 
		end if
	end tell
end main

on run
	my main("")
end run

on open s
	my main(s)
end open