Only if the user wishes to work with HFS paths or Applescript aliases rather than POSIX paths, and only marginally so because of the repeat loop that would be needed for the conversions.
While the shell solution presented above is robust owing to the find command’s extraordinary capabilities (of which my example, of course, barely scratches the surface), to my knowledge, the shell offers no easy way to convert from POSIX paths to HFS paths other than to “cheat” via an osascript command. Still, one can do amazing things (and amazing damage if used improperly) with the find command, all the more so if options such as -exec or -delete are incorporated. I would think it should be somewhere in one’s toolbox.
Thanks for the tip. I wasn’t aware of that potential problem.
Thanks for the nice handler that adds filtering to the System Events solution.
What a clever way to subselect items from an input list!
I was thinking that the text item delimiters property might be a particularly efficient way of filtering paths. I did a quick execution speed test comparing a variation of your method vs Nigel’s technique of nested repeat loops. I must admit that I fully expected the former to be faster, but lo and behold, the nested repeat loops turned out to be about 1.7 x faster:
tell application "System Events" to set hfsPaths to path of files of folder [HFS path to parent folder containing 600 files with various file endings, including "pdf", "txt", and others]
set extensionList to {".pdf", ".txt"}
on getPathsViaTID(hfsPaths, extensionList)
set tid to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to extensionList
set targetPaths to {}
repeat with thisPath in hfsPaths
tell thisPath's text items to if (length > 1) and (last item = "") then set end of targetPaths to thisPath's contents
end repeat
end try
set AppleScript's text item delimiters to tid
return targetPaths
end getPathsViaTID
on getPathsViaRepeatLoops(hfsPaths, extensionList)
set targetPaths to {}
repeat with thisPath in hfsPaths
tell thisPath's contents
repeat with thisExtension in extensionList
if (it ends with thisExtension's contents) then
set end of targetPaths to it
exit repeat
end if
end repeat
end tell
end repeat
return targetPaths
end getPathsViaRepeatLoops
-- Result: For 100 repetitions of each handler, getPathsViaRepeatLoops was about 1.7 x faster than getPathsViaTID!