Select similar files in a Finder window via AppleScript

Apr 18, '06 05:52:00AM

Contributed by: MohsanKhan

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.]

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20060412014652826