Select all files of same type

Sorry if this has been covered here before, I have spent a while with this on Google (which ought to address all forum posts here) and failed to find anything that works in the way I would like, and I haven’t been able to modify any of the scripts - being a total novice!

What I want to do is select a file or files in FInder, and use Services to select all files of the same extension in that folder. This would be useful at home on 10.7 as well as in the office with 10.6.8.

This script will allow me to perform multiple functions with these selections, for example deleting backup files or organising files of a type to copy/move them to other folders.

I have been trying to use the following script and, (most likely for very obvious reasons) utterly failed!

If anyone can fix this, or suggest a better or faster way, I would be very grateful!

on run {file_name, parameters}
	tell application "Finder"
		get file_name as string
		set selectedCount to count items in selectionList
		if selectedCount > 0 then
			set nameExtension to name extension of item 1 in selectionList
			select (every item where name extension ¬
				is nameExtension) of (file selected_file)
		end if
	end tell
end run


maybe this will get you started…

set theFolder to path to desktop

-- or
-- set theFolder to choose folder

set allFiles to list folder theFolder

-- or
tell application "Finder"
	set theSelection to selection
	-- then don't forget to parse the selection and list every folder in it
	-- so you end up with a list containing just files
end tell

-- you could of course use Automator and then save it as a service, 
-- but since I don't use Automator very often, I would have to look 
-- it up. Plain AppleScript should do just fine as well....

set filesWithExtensionPDF to filterFilesByExtension(allFiles, "PDF")

--> List of pdf files in folder

on filterFilesByExtension(theFiles, theExtension)
	set res to {}
	repeat with thisFile in theFiles
		ignoring case
			set thisFile to thisFile as text
			if thisFile ends with "." & theExtension then
				set end of res to thisFile
			end if
		end ignoring
	end repeat
	return res
end filterFilesByExtension



try this.

In the service I’ve selected “Service receives selected files or folders in Finder
The only action is a “Run AppleScript” action containing this code

on run {input, parameters}
	set extensionList to {}
	if (count input) > 0 then
		repeat with anItem in input
			tell application "System Events" to set nameExtension to name extension of anItem
			if nameExtension is not missing value and nameExtension is not in extensionList then
				set end of extensionList to nameExtension
			end if
		end repeat
		tell application "Finder"
			set parentFolder to container of item 1 of input
			select (every item of parentFolder whose name extension is in extensionList)
		end tell
	end if
	return input
end run

you can select a single file or multiple files with multiple file extensions.
The script doesn’t consider items without any file extension, e.g. folders

Thanks a lot guys!

The first response I was still working through when I received Stefan’s.

Unfortunately this doesn’t seem to work the same for all extensions. I have created a new Automator Service which receives selected files or folders in Finder, with the ‘Run Applescript’ action attached to it.

If I select a file with an msg extension, it works fine. If I select a file with an skb extension (SketchUp backup file), it selects all the files, but generates an error.

It’s also very slow, to the point that its use is limited. I timed 22 seconds to select all the png files on a network volume inside a folder with a total of 56 items.

Is this because the files are on a networked volume, or is it that the script isn’t quite as efficient as it could be?