Advocacy

  Myths
  Press

Dojo (HowTo)

  General
  Hack
  Hardware
  Interface
  Software

Reference

  Standards
  People
  Forensics

Markets

  Web

Museum

  CodeNames
  Easter Eggs
  History
  Innovation
  Sightings

News

  Opinion

Other

  Martial Arts
  ITIL
  Thought


The Need for Speed
Optimizing AppleScript's Performance


Tim Mityok <[email protected]>
Copyright © 1998-99 by Public Access Software

AppleScript, until Mac OS 8.5, was not known for its speed. There are, however, various tricks of the trade that can help to boost the speed of your scripts that are not covered in the various How-To Books. Not all of these tricks and tips will be appropriate for your needs, but they can come in handy when you need them.

Faster Text Manipulation

While working within AppleScript you are at its mercy for processing power. This is especially true for manipulating text strings in various ways. One of the most time-intensive processes you can do is to break down a piece of text into small chunks.

A very quick and simple way to accomplish this is to use a three-step process that is basically a pre-made shortcut for a recursive text-search operation:

In this example the first 2 lines will change the text delimiter, which is a key that is used to determine where to break text, to something useful. In this case we are using a TAB character which is a common delimiter for text files such as databases that have been exported from Excel or FileMaker Pro.

Notice that we save the previous text delimiter in a temporary variable. This way we can restore the delimiter to its previous settings when we are finished after all the delimiter may be something completely different than what we set it to and if it is not reset it will affect all subsequent text manipulating operations.

The next step is to ask AppleScript to break down the text into smaller parts that are separated by a TAB character. This one step is the key to the substantial speed boost we will get since AppleScript has its own internal text manipulation routine to perform this operation.

The text items are now in a list format:

 

Now that the text is broken down we restore the original text item delimiter.

If we were to recursively scan the text string looking for our TAB character and then grabbing the preceding characters into a single string it would take many times longer than the method above.

This process is accomplished entirely within AppleScript, no Scripting Additions are used. If you are distributing a script to others you may or may not be able to include the appropriate Scripting Addition to provide the needed functionality to your script.

You could get a somewhat faster text processor from a Scripting Addition which would be best for very large manipulations, but in most cases using AppleScript itself is more than adequate.

Share the Load

This leads into the next way to get a speed boost; using a Scripting Addition to shoulder the work of your script. Scripting Additions are available to provide many different features to a script such as displaying a Dialog Box, gathering information on the environment or to process data in some way.

 

Scripting Additions are processor-native software that AppleScript or other OSA-compliant languages can access as needed. There may be a small speed penalty for jumping to a Scripting Addition, especially on older PowerPC-based Macintosh computers since most Scripting Additions still come in 680x0 versions.

With that in mind the performance tradeoffs could be substantial. Scripting Additions are typically designed to only do one or more tasks. They do not have to worry about coexisting with the entire Mac OS as most application programs do so they can be very efficient in their work.

Using a Scripting Addition to manipulate text is substantially faster than using straight AppleScript in nearly all cases. In a few exceptions the speed differences may be immeasurable so it would be personal preference to use a Scripting Addition or not in those cases.

Scripting Additions are the only way to extend the AppleScript language itself beyond what it can already do, and the performance could be substantial in many ways. If you are to distribute your script to others you will need to include any non-Apple Scripting Additions with your software to allow others to make full use of it.

Reduce, Reuse, Recycle

Another way to help keep your script performing at a reasonable pace is to reduce the amount of processing your various routines have to do.

If your script is frequently accessing the same data from an application or a Scripting Addition, and the information should not change during the processing of the particular routine, create a temporary variable to hold the data and re-use that variable.

This will prevent AppleScript from having to repeatedly fetch the same data over and over again from an application or Scripting Addition. Each time you make a request for data you could be switching into 68k Emulator mode and then put even more demands on the System by having the application or Scripting Addition repeatedly process the same request.

Even though AppleScript itself is PowerPC-native in Mac OS 8.5, many of the programs you could be using or the Scripting Additions you have may still be in 68k form. Also, the Mac OS mechanism that that allows different programs to communicate with each other called the Apple Event Manager is still in 68k form, even in Mac OS 8.5.

All of the switching between PowerPC and 68k mode can bog down the Mac OS while the script is being executed. There's not much you can do about that, its up to Apple to move their Operating System to the PowerPC platform. You can, however, use the techniques described above to improve performance as much as possible to get more work done or provide a more peppy user experience.


Reference URLs:

  1. Scripting Additions list <http://www.scriptweb.com/temp/osaxen/>


Created: 02/22/99
Updated: 11/09/02


Top of page

Top of Section

Home