Wednesday, January 19, 2022

#1 2005-10-12 06:26:58 am

From:: Malasaña, Madrid
Registered: 2002-11-20
Posts: 2014

Repeat loop vs filter reference - when?

Repeat loops are powerful, but there are often faster, cleaner ways to get jobs done. Suppose you want to move every file from folder A whose name begins with "May" to folder B, replacing old versions with newer ones. The repeat statement would look like this:


tell application "Finder"
   set fileList to every file of folder "A"
   repeat with aFile in fileList
       if name of aFile begins with "May" then
           move aFile to folder "B" with replacing
       end if
   end repeat
end tell

Now consider the filtered form; it distills down to one line:


tell application "Finder" to move (every file of folder "A" whose name begins with "May") to folder "B"

Running each of these also shows that the second version executes much faster than first when the folders contain many files. So while repeat loops are extremely powerful and often necessary, try to first think if simply using a filter reference might get things done simpler.

Filed under: Finder



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)