Finder searches


I am trying to find a specific file using applescript. I am finding usinf finder with applescript is extremely slow. DOes Anyone know why this is?

tell application "Finder"
	with timeout of 600 seconds
		set d to first file of entire contents of (choose folder) whose name contains "PGR01018"
	end timeout
end tell

The volume I am searching is extremely large but if i manually search for the file with finder it only takes couple of seconds where as with aplescript it takes minutes


There is a quicker method for searching with AppleScript, but searching with unix is the quickest. Here’s an example:

set find_dir to quoted form of POSIX path of (path to "cusr")
set file_name to quoted form of "iTunes Library"
do shell script "find " & find_dir & " -iname " & file_name

It just searches you Home directory for an item named “iTunes Library”.


Unix example above is quick but you have to convert to finder paths and built list with AppleScript’s text item delimiters, an easier way is using Automators search or spotlight. It return a Finder compatiable list. The only thing is you can’t script your search option, but I believe that changes in Leopard.

Thanks for the answers. I just wonder why finder is so slow using applescript to command it because when I search by simply typing in the search in the finder window it’s incredibly fast. It’s like finder doesn;t use it;s index when commanded by applescript.

The do shell script command works great but how do I set the found file back to another variable.
I can do set d to result. But that doesn’t seem towork in Applescript studio which is where the main script is going to be run from.


Got it figured out now dudes. Thanks for your help
set d to POSIX path of (choose folder)
set find_dir to quoted form of POSIX path of d
set file_name to quoted form of “PGR01020*”
set the_result to (do shell script "find " & find_dir & " -iname " & file_name)
set the_s to (POSIX file the_result) as alias
tell application “Finder”
copy file the_s to folder (choose folder)
end tell

I think you still need to use ‘duplicate’ instead of copy.


Ite result can be multiple files or none. So…

set find_dir to quoted form of POSIX path of ((choose folder) as string)
set file_name to quoted form of "idea*"
set the_result to do shell script ("find " & find_dir & " -name " & file_name)
if not the_result = "" then
	set the_list to paragraphs of the_result
	set destination to (choose folder)
	repeat with the_s in the_list
		set the_s to POSIX file (the_s as string)
		tell application "Finder"
			set the_d to duplicate the_s to destination
			set comment of the_d to the_s
		end tell
	end repeat
end if

I set the spotlight comment of the duplicated file to the source path/file, so like an alias you know where it come from.

Note the find returns sometimes double / like ‘/users/me//desktop/’ strange. if you do the ‘POSIX file’ line outside the Finder it works, if inside Finder then you get an error. Its does the same double / in terminal so I guess its standard. Maybe someone can tell us why.

Have fun.