Section 13.7. String


13.7. String

A string is the basic text datatype. It has the MacRoman encoding; see "Unicode Text," later in this chapter, for more about the implications of this. Strings are your primary medium for communicating text information to a scriptable application. AppleScript provides some basic string operators (discussed in Chapter 15).

A literal string is delimited by quotation marks:

 set s to "howdy" class of s -- string

The empty string is symbolized by "".

In typing a string literal, you may enter certain characters in "escaped " form; they are listed in Table 13-1. These are the only "escaped" characters.

Table 13-1. "Escaped" string literals

What to type

ASCII equivalent

Result

\"

ASCII character 34

Quotation marks

\t

ASCII character 9

Tab

\r

ASCII character 13

Return

\n

ASCII character 10

Newline

\\

ASCII character 92

Backslash


Other untypeable characters may be generated using the ASCII character scripting addition command and incorporated into a string by concatenation (see chapters 15 and 21). There are also a few global properties expressing character values (listed in Chapter 16); these too can be incorporated into a string by concatenation.

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.

13.7.1. 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"

13.7.2. 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, the number of s's in Mississippi

The value of the text item delimiters persists as long as this instance of the AppleScript scripting component does. Because you might run more than one script in the presence of this scripting component, any of which might set the text item delimiters, it is wise to make no assumptions as to the value of the text item delimiters. In other words, don't use it without setting it first. Apple's documentation makes a big deal of this, but it's really no different from any of the other AppleScript global properties, such as pi (see Chapter 16).

Observe that other string elements may equally be used to split a string, often more conveniently: characters splits a string into individual characters, words splits a string at its word boundaries, and paragraphs splits a string at its line breaks.




AppleScript. The Definitive Guide
AppleScript: The Definitive Guide, 2nd Edition
ISBN: 0596102119
EAN: 2147483647
Year: 2006
Pages: 267
Authors: Matt Neuburg

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net