Targeting and alias-making script and disregarding folders

Hey all. I’ve come up with a script - I’ve introduced it before on here - which is meant to perform the following task: Make aliases of all files in a given directory that do not appear on a delimitted tabbed text file. Basically, the script knows to exclude the file names in that .txt file while making aliases of every other file in the chosen directory and spitting the aliases out into a common folder.

I had success with it in limited testing, but in more complicated file structures with more than a few folders and several levels, it seems indiscriminate. I need to have it just target the names of files NOT appearing on that list rather than all the folders in that directory and everything in them. I think this is the problem that causes the script to provide so many false positives to the point of being worthless - but am I also missing something else here?

I’ve posted the script below. It’s very short and to-the-point, at least for a script I’ve coded myself. I may have approached it in a klunk way though. Thanks for any help or insight you might have:

tell application "Finder"
	set folderRef to (make new folder at desktop with properties {name:"Not In Their List"})
	set fileRef to (choose folder)
	set textFile to (choose file of type "TEXT") as text
	
	-- Exclude list
	set excludedList to paragraphs of (read file textFile)
	
	
	set pSF to quoted form of POSIX path of fileRef
	set tPaths to {}
	repeat with aFile in excludedList -- a form that's faster because you don't have to count the list
		try
			set end of tPaths to POSIX file (do shell script "mdfind -onlyin " & pSF & space & quoted form of aFile) as alias
		on error -- no file by that name was found in the searchfolder.
			set end of tPaths to aFile & " N/A"
		end try
	end repeat
	
	set allList to every file of fileRef whose name is not in excludedList
	
	make new alias file at folderRef to every item of allList
	
	
	
	
end tell

Using the mdfind is slow and IMO is slow, and not needed.
You can compare the displayed name items of the chosen folder to the items of excludelist.

The script below is how I where I would start with a script like this.
Also note the Make Alias is not looking at a folder, but at a list. which enforces the script to only alias those file in the list.


tell application "Finder"
	set folderRef to (make new folder at desktop with properties {name:"Not In Their List"})
	
	set fileRef to (choose folder)
	-- Exclude list
	set excludedList to paragraphs of (read file ((choose file) as text))
	set gName to items of fileRef whose displayed name is not in excludedList
	(* Use this line   --> 
	set gName to items of fileRef whose class is not folder and displayed name is not in excludedList
	<-- instaed of the above one, if you do not want aliases of folders 
	*)
	make new alias file at folderRef to items of gName
	
end tell

Oops, just notice i left in some errant quotes. Taken out,

That’s a really elegant way to do it I think. Thank you.

Is there a way to make it search recursively over a multilevel directory without having it basically stall out?

Do you mean like this?

tell application "Finder"
	set folderRef to (make new folder at desktop with properties {name:"Not In Their List"})
	set fileRef to (choose folder)
	-- Exclude list
	set excludedList to paragraphs of (read file ((choose file) as text))
	(* will include items of sub folders, but will not make an alias the folders *)
	set gName to items of entire contents of fileRef whose class is not folder and displayed name is not in excludedList
	make new alias file at folderRef to items of gName
end tell