Tuesday, December 12, 2017

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

julifos
Administrator
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:

Applescript:

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:

Applescript:

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)