Do Something With the Finder Selection in AppleScript
Here’s a little code snippet that I often need. It basically iterates over the Finder selection individually and does something with the file. I use this to, e.g., label Finder items with a color using Keyboard Maestro.
tell application "Finder" try repeat with currentFile in items of (get selection) if condition of currentFile is "" then set something of currentFile to something else set something of currentFile to something else end if end repeat on error e return e end try end tell
e at the end has the advantage that you can notify yourself in case the script has had problems.
This snippet is currently the shortest, and most efficient, way I know of.
Label Finder Items
Just for completeness. Here’s one of my Label Finder Items scripts:
(* No color = 0 Orange = 1 Red = 2 Yellow = 3 Blue = 4 Purple = 5 Green = 6 Gray = 7 *) set mycolor to 7 tell application "Finder" try repeat with currentFile in items of (get selection) if label index of currentFile is not mycolor then set label index of currentFile to mycolor else set label index of currentFile to 0 end if end repeat on error e return e end try end tell