There has to be a better way

I am trying to get a count of all images in a folder and it’s subfolders. Is there a better/faster way than this?


set theFolder to choose folder {}
set thenumber to 0
set thecountertotal to 0
set thecountertotal to Repeatfolders(theFolder)
Display Dialog thecountertotal as string

on Repeatfolders(theFolder)
	set my thenumber to (my thenumber) + countitems(theFolder)
	tell application "Finder"
		set these_folders to every folder of folder theFolder whose name does not start with "."
	end tell
	repeat with i from 1 to count of these_folders
		set myItem to item i of these_folders
		set folderToBeCounted to myItem as string
		Repeatfolders(folderToBeCounted)
	end repeat
	return my thenumber
end Repeatfolders

on countitems(theFolder)
	tell application "Finder"
		set these_files to every file of folder theFolder whose name does not start with "." and (file type is "TIFF" or file type is "JPEG" or name extension is "tiff" or name extension is "tif" or name extension is "jpeg" or name extension is "jpg")
	end tell
	return (count of these_files)
end countitems

maybe you can try something like this:

tell application "Finder"
	set afolder to choose folder
	(every file of (entire contents of afolder)) whose (kind is "JPEG Image") -- or file type is "TIFF" ...etc.
end tell

Either of these should work. (However, using the Finder can be slow for large folders.)

tell application "Finder"
	launch
	choose folder
	files of (entire contents of result) whose ¬
		name extension is "jpg" or ¬
		name extension is "jpeg" or ¬
		file type is "JPEG" or ¬
		name extension is "tif" or ¬
		name extension is "tiff" or ¬
		file type is "TIFF"
	count result
end tell

choose folder
get quoted form of (text 1 thru -2 of (POSIX path of result))

do shell script "/usr/bin/find " & result & " -iname *.jpg -or -iname *.jpeg -or -iname *.tif -or -iname *.tiff"
count paragraphs of result

Note that the second script will search inside of bundles (e.g. some applications and document formats).