AppleScript_ The Definitive Guide - Matt Neuburg [123]
After compilation, any tab, return, and newline characters within a literal string are unescaped and turned into whitespace ; they remain intact, but you can no longer see directly what characters they are, which is a pity (see "Lines" in Chapter 5). Script Debugger, however, can display such "invisibles."
Don't confuse AppleScript's built-in string type and its native manipulations of this type with how scriptable applications implement their own string behavior. When you ask an application to perform manipulations on text of its own, it might behave differently from AppleScript. For example:
tell application "Tex-Edit Plus"
set text of window 1 to "Now is the winter"
get word after character 3 of text of window 1 -- "is"
end tell
get word after character 3 of "Now is the winter" --error: Can't get word after "w"
In the tell block, everything belongs to Tex-Edit Plus; you're speaking of Tex-Edit's implementation of the text class, and you're taking advantage of Tex-Edit's idea of a word and a character and what can be done with them. In the last line, you're working with a string and talking to AppleScript itself.
String Properties
The following are the properties of a string. They are read-only.
length
The number of characters of the string. You can get this same information by sending the count message to the string.
quoted form
A rendering of the string suitable for handing to the shell as an argument to a command. The string is wrapped in single quotation marks and internal quotation marks are escaped.
You probably shouldn't look at the result of quoted form, because you might not understand it; it's meant for the shell's eyes, not yours, and an extra level of (mis)representation is added by AppleScript as it shows you the string. For example:
quoted form of "life's a \"bowl\" of cherries"
-- "'life'\\''s a \"bowl\" of cherries'"
That looks pretty dreadful, but it's right, as you'll discover if you hand it to the shell:
set s to quoted form of "life's a \"bowl\" of cherries"
do shell script "echo " & s -- "life's a \"bowl\" of cherries"
String Elements
The following are the elements of a string. Bear in mind that you can't set them; you cannot mutate a string in place! Elements may be specified by index number, by range, or with every.
character
A string representing a single character of the string.
word
A string representing a single word of the string. It has no spaces or other word-boundary punctuation.
paragraph
A string representing a single paragraph (or line) of the string. It has no line breaks. AppleScript treats a return, a newline, or both together (CRLF) as a line break.
text
A run of text. Its purpose is to let you obtain a single continuous string using a range element specifier; see "Range" in Chapter 11. So, for example:
words 1 thru 3 of "Now is the winter" -- {"Now", "is", "the"}
text from word 1 to word 3 of "Now is the winter" --"Now is the"
text item
A "field" of text, where the field delimiter is AppleScript's text item delimiters property.
The text item property needs some explanation. There is a global property (see Chapter 16) called text item delimiters. You can set this to any string you like. (The documentation claims that the text item delimiters is a list of strings, but in fact only the first item of the list is effective.) When you speak of a text item of a string, the current value of the text item delimiters is used to "split" the string; no text item contains the value of the text item delimiters, and the number of text items of a string is exactly one more than the number of times it contains the value of text item delimiters. For example:
set text item delimiters to ":"
text items of "feathers:Users:mattneub" -- {"feathers", "Users", "mattneub"}
set text item delimiters to "tt"
text items of "Matt" -- {"Ma", ""}
set text item delimiters to "s"
set howMany to (count text items of "Mississippi") - 1
howMany --4,