Finder getting the first 10 files with the .txt extension

Hi all,

Newbie applescripter here and I really need help :slight_smile: I’ve been ‘coding by example’ searching through google for different Applescript samples, but this one is eluding me.

I’m trying to get the finder to return say 5 .txt files from a folder. Here’s my attempted code :

	tell application "Finder"
		set thePath to folder MyFolder
		set txtFileList to files 1 thru 5 in thePath whose name ends with ".txt"
	end tell
	return txtFileList

(MyFolder is a folder name passed into this function)
I am able to get every file with .txt extension - i.e. this works fine:

	set txtFileList to every file in thePath whose name ends with ".txt"

But for some reason i cant get it to limit the results to a fixed number of files. (There are more than 5 files in the folder)

I’m using this snippet to process text files one at a time but the script takes too long when processing all files, so i need to limit the processing down to manageable chunks.

Can anyone pleeease help?

Thanks

Drav

Hi Drav,

welcome to MacScripter.
You’re probably talking about OS X, so please post in the OS X forum

Try this, I added some error handling, if there are less than 5 text files in the folder


tell application "Finder"
	set thePath to folder MyFolder
	set theFiles to files of thePath whose name ends with ".txt"
	if (count theFiles) < 5 then
		copy theFiles to txtFileList
	else
		set txtFileList to items 1 thru 5 of theFiles
	end if
end tell
return txtFileList