Search list, copy out relevant entry

I have some AppleScript that generates a list of file names in a folder…

tell application "Finder"
	update folder folderpath
	set fileNamesList to list folder folderpath without invisibles
end tell

Somewhere in the file name is a number, I would like to search the list for the file name that contains this number and then copy that file name out to a variable. After thinking about it some I came up with this, seems to work, but I don’t trust myself…

tell application "Finder"
	update folder folderpath
	set fileNamesList to list folder folderpath without invisibles
	repeat with i from (count the fileNamesList) to 1 by -1
		set theFile to item i of fileNamesList
		if theFile contains OrderNumberFromCell then exit repeat
	end repeat
end tell

This leaves me with the file name in theFile that I can team up with the folder path later on in the script to process just that file. But like I said, I’m unsure.

Hi wplate,

You can just use the filter reference form.

set OrderNumberFromCell to “0012”
tell app “Finder”
set theFile to first file of some_folder whose name contains OrderNumberFromCell
end tell

gl,

Or if you just want the name of that file we can add to Kel’s solution:

tell application "Finder" to set GotIt to name of first file of (choose folder) whose name contains "XYZ"

The thing to remember about filter forms is that we have to look at the filtered object one item at a time, hence the “first file”