Saturday, October 25, 2014
  • Index
  •  » Core Language
  •  » What are AppleScript's Text Item Delimiters and how are they used?

#1 2005-10-12 07:33:04 am

julifos
Administrator
From: MalasaƱa, Madrid
Registered: 2002-11-20
Posts: 2014

What are AppleScript's Text Item Delimiters and how are they used?

AppleScript's Text Item Delimiters give you a way to break up text (or "parse" in computer jargon) into pieces, and then extract data from those pieces. They are the separators of text items in a piece of text. A simple example:

Applescript:

set someText to "Where.the.Wild.Things.Are "
set AppleScript's text item delimiters to {"."}
set delimitedList to every text item of someText

When run, the text someText ("Where.the.Wild.Things.Are") is broken up at every occurrence of a period (the "text item delimiter"), and the delimitedList is a list composed of all of the broken pieces: {"Where", "the", "Wild", "Things", "Are"}

Another use might involve pulling an email address out of HTML:

Applescript:

set someText to "mailto:bill@macscripter.net "

set AppleScript's text item delimiters to {":"}
set emailAddress to text item 2 of someText

In this case the delimiter, a colon, separates "mailto:bill@macscripter.net" into {"mailto", "bill@macscripter.net"} and text item 2 of the list yields the email address.

It's a good practice to capture the state of AppleScript's text item delimiters at the beginning and restore them at the end as once they are changed they will globally affect the running environment until the process is quit and restarted! (eg, the script editor, or your script running as standalone application)

This approach works best:

Applescript:

try
   set oldDelims to AppleScript's text item delimiters -- save their current state
   set AppleScript's text item delimiters to {":"} -- declare new delimiters
   -- do script steps here
   set AppleScript's text item delimiters to oldDelims -- restore them
on error
   set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
end try

For an interesting article regarding text item delimiters, go here.

Offline

 
  • Index
  •  » Core Language
  •  » What are AppleScript's Text Item Delimiters and how are they used?

Board footer

Powered by FluxBB

[ Generated in 0.034 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)