Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Select similar files in a Finder window via AppleScript Desktop
Ever wanted to quickly select all similar types of files among a bunch of others in a Finder window? I often find myself having to select, for instance, all the JPEG images amoung docs and movies, etc.

I don't want to change the view, sort by type, and then manually select all the JPEGs; that's so 90's! Instead, try this AppleScript:
try
  tell application "Finder" to set the source_folder ¬
   to (folder of the front window) as alias
on error -- no open folder windows
  --set the source_folder to path to desktop folder as alias
  --problem is a window can be open but out of focus
  beep
end try

tell application "Finder"
  set selectionList to {} & selection as list
  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 (folder source_folder)
  end if
end tell
[robg adds: This is a very handy timesaver, even in this era of Spotlight. To use the script, enter it in Script Editor, and save it to your user's Library -> Scripts folder. Make sure the Scripts menu is enabled (10.3: Applications -> AppleScript -> Install Script Menu; 10.4: Applications -> AppleScript -> AppleScript Utility). Once saved into the Scripts folder, switch to the Finder and select an item whose type you'd like to select, then select the script you saved from the Scripts menu. Bingo, all similar types will be selected.

I also turned the script into an Automator plug-in for the Finder. Launch Automator and pick the Automator action, and drag Run AppleScript from Library into the work area. Paste the above code, then choose File: Save as Plug-in, and make sure it's set as a Finder plug-in. This works well, except that the chosen folder loses focus at the end of the action, which doesn't happen if you run the script natively.]
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[16,356 views]  

Select similar files in a Finder window via AppleScript | 11 comments | Create New Account
Click here to return to the 'Select similar files in a Finder window via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Select similar files in a Finder window via AppleScript
Authored by: fds on Apr 18, '06 07:45:56AM

Alternatively, you may simply drop the AppleScript file you made on the Finder window's toolbar to create a convenient toolbar button for it.



[ Reply to This | # ]
Select similar files in a Finder window via AppleScript
Authored by: rapdigital on Apr 18, '06 08:58:26PM

Very Cool. I saved the script as an app, with the start up screen option unchecked, and dropped it on the window tool bar and it works a treat. Thanks



[ Reply to This | # ]
Select similar files in a Finder window via AppleScript
Authored by: syko on Apr 18, '06 09:11:40AM

Very nice!

Now, how do you make it do recursive into subfolders? :D



[ Reply to This | # ]
Take a look at PathFinder for this as well
Authored by: dldestwolinska on Apr 18, '06 11:17:41AM

As a side note, PathFinder v4.x has this functionality built in. It's very handy at times.



[ Reply to This | # ]
Fixed version to match all the different kind of files selected
Authored by: Lutin on Apr 19, '06 02:01:19AM
This idea is pure genius. I wish I had it earlier, It would have saved me a lot of time.

Here is my own adapatation.
- It now rely on the kind of a file, not its extension (then, if you have img1.jpg and img2.JPG, it'll work properly).
- If several files are selected, the final selection will match all the kind of file present in the inital selection (example: if you select text1.txt, archive.gzip and folder1, it'll select all the txt and gzip files and the folders).


-- In column mode, if only folders are selected, it doesn't work as expected
-- The selection is only the last folder of the window.
-- Works fine in other view mode
try
	tell application "Finder" to set the source_folder ¬
		to (folder of the front window) as alias
on error -- no open folder windows
	--set the source_folder to path to desktop folder as alias
	--problem is a window can be open but out of focus
	beep
end try

tell application "Finder"
	set kindList to {}
	set toselectList to {}
	
	-- Get the current selection
	set selectionList to {} & selection as list
	
	-- Be sure the selection isn't empty
	set selectedCount to count items in selectionList
	if selectedCount > 0 then
		
		-- Get all kind of files selected
		repeat with list_item in selectionList
			if (kind of list_item) is not in kindList then
				set end of kindList to kind of list_item
			end if
			
		end repeat
		
		-- Get all the elements of the folder
		set allList to (every item of folder source_folder)
		
		-- Add the files matching to kind selected to the list allList
		repeat with file_item in allList
			if kind of file_item is in kindList then
				set end of toselectList to file_item
			end if
		end repeat
		
		-- Select the files
		select (every item of toselectList)
	end if
end tell

There is one bug I couldn't fix: If you are in column view mode, if only folders are selected, it doesn't work as expected.
The final selection is only the last folder of the window.
It works fine in other view mode.

If anyone as any idea of how to fix this, please tell me.

[ Reply to This | # ]
Fixed version to match all the different kind of files selected
Authored by: ret on Apr 19, '06 02:38:43AM

Haven't got the answer to your bug, but just wanted to say the concept and your solution are terrific.

Thanks

cheers
RET

---
perl -e 'require Signature.pm; srand; printf STDOUT "%s\n", $Signature[rand @Signature];'



[ Reply to This | # ]
work around for the bug
Authored by: mzs on May 26, '06 06:53:36AM
There is a work-around foir the column view bug in the hint: Select files in Finder from Terminal. Scroll down for the comment titled:

  fix for column view bug

Is there a way to link to individual comments at macosxhints?

[ Reply to This | # ]

Select similar files in a Finder window via AppleScript
Authored by: Lutin on Apr 20, '06 06:39:43AM
I liked that idea so much that I made an other version which work with the name of the file (stripped of its extension).
A dialog box lets the possibility to modify the string if necessary.

I binded it to Command-F2 with Quicksilver, and it's a huge time-saver.

-- In column mode, if the final selection is only folders, it doesn't work as expected
-- The selection is only the last folder of the window.
-- Works fine in other view mode
try
	tell application "Finder" to set the source_folder ¬
		to (folder of the front window) as alias
on error -- no open folder windows
	--set the source_folder to path to desktop folder as alias
	--problem is a window can be open but out of focus
	beep
end try

tell application "Finder"
	set toselectList to {}
	
	-- Get the current selection
	set selectionList to {} & selection as list
	
	-- Be sure the selection isn't empty
	set selectedCount to count items in selectionList
	if selectedCount > 0 then
		
		-- Get the first file selected
		set thefile to item 1 of selectionList
		
		-- Get the file of the name without its extension
		set filename to name of thefile
		
		set fileextension to name extension of thefile
		if (fileextension is not "") then
			set ix to offset of fileextension in filename
			set filename_withoutext to characters 1 thru (ix - 2) of filename as string
		else
			set filename_withoutext to filename
		end if
		
		
		-- Ask for the string to match	
		set matching_str to display dialog "Set the string you want to match:" default answer (filename_withoutext) buttons {"Cancel", "Continue"} default button "Continue"
		
		-- Get all the elements of the folder
		set allList to (every item of folder source_folder)
		
		-- Add the files with name matching to the list allList
		repeat with file_item in allList
			if (text returned of matching_str) is in name of file_item then
				set end of toselectList to file_item
			end if
		end repeat
		
		-- Select the files
		select (every item of toselectList)
	end if
end tell

I still have the same bug, and no idea how to fix it.

[ Reply to This | # ]
Select similar files in a Finder window via AppleScript
Authored by: osxpounder on Apr 21, '06 12:26:58PM

I tried the "Kind" script. It's hit or miss. I find that it won't select any .xls files, which my Mac already knows as Excel spreadsheets ["Kind: Microsoft Excel workbook"] ....

---
--
osxpounder



[ Reply to This | # ]
Select similar files in a Finder window via AppleScript
Authored by: MacBeliever on May 06, '10 06:39:56PM

I use this two compare files in two separate windows, selecting similar or different items. Probably needs a little fixing, but it works for me.
global fileListWindow1
global fileListWindow2
global nameListWindow1
global nameListWindow2
tell application "Finder"
activate me
set windowCount to count every window
if windowCount is less than 2 then
display dialog "You need to have at least two windows open"
return
end if
set nameExtensionQuestion to display dialog "Do you want to ignore the name extension when comparing?" buttons {"yes", "no", "cancel"} default button {"yes"}
set theAnswer2ExtensionQuestion to button returned of nameExtensionQuestion
if theAnswer2ExtensionQuestion is equal to "yes" then
set considerNameExtension to true
else if theAnswer2ExtensionQuestion is equal to "no" then
set considerNameExtension to false
end if
activate me
set theQuestion to display dialog "Do you want to select different or similar items?" default button "differences" buttons {"differences", "similarities", "cancel"}
set theReply to button returned of theQuestion
with timeout of 100000 seconds
set oldDelimiters to AppleScript's text item delimiters
set errorList to {}
set fileListWindow1 to {}
set fileListWindow2 to {}
set nameListWindow1 to {}
set nameListWindow2 to {}
activate
set idList to my dockNonDockWindowCount(windowCount)
set idWindow1 to item 1 of idList
set idWindow2 to item 2 of idList

-- have to do this check because in filevault, this fails: tell window id idWindow1
if (get id of window 1) is not idWindow1 then
my switchWindows(idWindow1)
end if
set countWindow1 to count every item of window 1
if (get id of window 1) is not idWindow2 then
my switchWindows(idWindow2)
end if
set countWindow2 to count every item of window 1

--inventory both windows
my switchWindows(idWindow1)
set theFilesi to my getAliases(idWindow1)
set theNamesi to my getNames(theFilesi, considerNameExtension)
my switchWindows(idWindow2)
set theFilesj to my getAliases(idWindow2)
set theNamesj to my getNames(theFilesj, considerNameExtension)
my switchWindows(idWindow1)

-- match and compare things between the windows
if theReply is "differences" then
set choiceIsSimilarities to false
else if theReply is "similarities" then
set choiceIsSimilarities to true
end if
my gatherTargetItems(countWindow1, countWindow2, theNamesi, theNamesj, theFilesi, theFilesj, choiceIsSimilarities)
set AppleScript's text item delimiters to oldDelimiters
activate

--select items in both windows
if (get id of window 1) is not idWindow1 then
my switchWindows(idWindow1)
end if
tell window 1 to select {}
select fileListWindow1
my switchWindows(idWindow2)
tell window 1 to select {}
select fileListWindow2
--set bigList to {}
--set end of bigList to my nameListWindow1 & my nameListWindow2
display dialog "Done"
end timeout
end tell

--lists window items that are either similar or different, depending on user's choice
on gatherTargetItems(countWindow1, countWindow2, theNamesi, theNamesj, theFilesi, theFilesj, choiceIsSimilarities)
tell application "Finder"
if (count theNamesi) is less than 1 then
display dialog "nono"
end if
repeat with counter from 1 to (countWindow1)
set itIsThere to false
repeat with secondCounter from 1 to (countWindow2)
if item counter of my theNamesi is equal to item secondCounter of my theNamesj then
set itIsThere to true
exit repeat
end if
end repeat
if itIsThere is choiceIsSimilarities then
try
set end of fileListWindow1 to item counter of my theFilesi
set end of my nameListWindow1 to item counter of my theNamesi
end try
end if
end repeat
repeat with counter from 1 to (countWindow2)
set itIsThere to false
repeat with secondCounter from 1 to (countWindow1)
if item counter of my theNamesj is equal to item secondCounter of my theNamesi then
set itIsThere to true
exit repeat
end if
end repeat
if itIsThere is choiceIsSimilarities then
try
set end of fileListWindow2 to item counter of my theFilesj
set end of my nameListWindow2 to item counter of my theNamesj
end try
end if
end repeat
end tell
end gatherTargetItems

--gets path to every item in window
on getAliases(windowId)
tell application "Finder"
set everyItem to {}
if (get id of window 1) is not windowId then
my switchWindows(windowId)
end if
if (count window 1) is greater than 1 then
if (get id of window 1) is not windowId then
my switchWindows(windowId)
end if
set everyItem to every item of window 1 as alias list
else
set end of everyItem to (item 1 of window 1 as alias)
end if
ignoring application responses
tell application "Finder" to set selection to {}
end ignoring
end tell
return everyItem
end getAliases

--gets name of every item in window
on getNames(everyItem, considerNameExtension)
set allFiles to {}
repeat with counter in everyItem
tell application "Finder"
set displayedName to get name of (get info for counter)
if considerNameExtension is true then
set displayedName to my bust(displayedName)
end if
set end of allFiles to displayedName
end tell
end repeat
return allFiles
end getNames

--moves second window frontmost
on switchWindows(windowId)
tell application "Finder" to activate
tell application "Finder"
repeat until windowId is equal to (get id of window 1)
my flip()
end repeat
end tell
end switchWindows

-- makes sure windows are active and not minimized in the dock
on dockNonDockWindowCount(windowCount)
tell application "Finder"
set notCollapsedSet to {}
set loopCounter to 0
repeat with counter from 1 to (windowCount)
set windowInfo to get properties of window counter
if collapsed of windowInfo is false then
set loopCounter to loopCounter + 1
set end of notCollapsedSet to id of window counter
if loopCounter is 2 then exit repeat
end if
end repeat
set activeCount to count notCollapsedSet
if activeCount is less than 2 then
display dialog "You must have two windows active. Either open a new window or activate a minimized window from the dock"
tell me to quit
end if
end tell
return notCollapsedSet
end dockNonDockWindowCount

-- switch from one window to another
on flip()
tell application "Finder" to activate
tell application "System Events"
key code 50 using {command down}
end tell
end flip

--converts Finder items into aliases
on finderDocListToAliasList(finderDocList)
if (count finderDocList) is greater than 1 then
set aliasList to {}
repeat with i in finderDocList
set end of aliasList to finderDocToAlias(i)
end repeat
return aliasList
else
return finderDocToAlias(item 1 of finderDocList)
end if
end finderDocListToAliasList

on listThem(theFiles, theNames, myList, nameList)
set end of myList to item counter of theFiles
set end of nameList to item counter of theNames
end listThem

-- gets name of file without the extension
-- kinda copied from Matt Neuburg's book
on bust(s)
set text item delimiters to "."
set pathParts to text items of s
set pathPartsCount to count pathParts
if pathPartsCount is greater than 2 then
set newName to items 1 thru -2 of pathParts
else if pathPartsCount is less than 3 then
set newName to item 1 of pathParts
end if
set newName to newName as string
return {newName}
end bust



[ Reply to This | # ]
Select similar files in a Finder window via AppleScript
Authored by: tedw on May 07, '10 09:31:16AM
just a tweak on the original script: this script locates the active window - including the desktop - more reliably, and allows choosing more than one type of file by selecting 2 or more different file types in the Finder Window.
tell application "Finder"
	set the sourceFolder to insertion location
	set selectionList to selection as list
	if selectionList is not {} then
		set extensionList to {}
		repeat with thisFile in selectionList
			if name extension of thisFile is not in extensionList then
				set end of extensionList to name extension of thisFile
			end if
		end repeat
		select (every item of sourceFolder whose name extension is in extensionList)
	end if
end tell


[ Reply to This | # ]