Applescript: move all files with a certain extension to the trash

This is very straightforward; it moves myfile.WMA to the trash:

tell application "Finder"
    move POSIX file "/Volumes/DS_30/myfolder/myfile.WMA" to trash
end tell

But how would I move all files in myfolder with a extension of .WMA to the trash?


Solution 1:

Using your example volume and folder name:

tell application "Finder" to ¬
    move (every item of ¬
        container (alias "DS_30:myfolder") of ¬
        application "Finder" whose name extension = "WMA") to trash

Solution 2:

You can use AppleScript's filter reference form - for example:

set myFolder to (choose folder)
tell application "Finder"
   try
      set theItems to files of myFolder whose name extension is "wma"
   on error errmess -- shell script
      log errmess
      set theItems to paragraphs of (do shell script "/usr/bin/find " & quoted form of POSIX path of myFolder & " -depth 1 -iname '*.wma' -print")
      repeat with anItem in theItems
         set contents of anItem to anItem as POSIX file as alias
      end repeat
   end try
   move theItems to the trash
end tell

Note that the filter reference works for application objects, not regular AppleScript objects such as lists and records.

Edit

Added a statement to perform a find shell script in the event of an error.