Thursday, November 23, 2017

#1 2005-10-12 02:40:17 pm

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

How can I improve my script's performance?

Beginners often (smartly!) use AppleScript's recording feature to write scripts. This is an excellent learning tool, but adds a great deal of code that can be pruned to make scripts run faster.

For example, if you record moving a file on the desktop to the trash, you end up with:


tell application "Finder"
   select file "aFile"
   delete selection
end tell

Whereas experience teaches you that one line would do the same thing:


tell application "Finder" to delete "aFile"

Another time saver is avoiding loops whenever possible. Suppose, for example, you had a folder on your desktop containing 1000 images, each ending with either .tif or .jpg, and you had to throw all of the .tif images in the trash. This loop would be a logical approach:


tell application "Finder"
   repeat with anImage in folder "Images" of desktop
       if name of anImage contains ".tif" then
           delete anImage
       end if
   end repeat
end tell

This works, but takes a long time to execute. Consider the almost instant alternative:


tell application "Finder" to delete (every file of folder "Images" whose name contains ".tif")

Generally, tweaking AppleScript performance is simply a matter of approach and experience.

Filed under: Finder



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)