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:

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:

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:

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.