AppleScript Speed:Text Handling

Another speed stunner, but not so much, as AppleScript’s text manipulation is acknowledged to be quite slow.

The objective is to extract a piece of text from a string, specifically the ‘middle’ of the OS X version. This is part of a larger script that needs to know which version of OS X it is running on.

Here are two ways of getting that piece of text. First, using the offset command:

set osVers to (do shell script "sw_vers -productVersion")
set aDot to (offset of "." in osiers)
set myVers to (text (aDot + 1) thru -1 of osiers)
set aDot to (offset of "." in myVers)
set myVers to (text 1 thru (aDot - 1) of myVers)

Second, using text item delimiters:

set osVers to (do shell script "sw_vers -productVersion")
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set myVers to (text item 2 of osiers)
set AppleScript's text item delimiters to oldDelims

Using text item delimiters is about 20 times faster.

September 13, 2011

google_pluslinkedingoogle_pluslinkedinby feather

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.