DekGenius.com
[ Team LiB ] Previous Section Next Section

13.4 String

A string is the basic text datatype. A literal string is delimited by double quotation marks:

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

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; other untypeable characters may be concatenated into the string by means of the ASCII character scripting addition command. (See Section 15.5 and Section 20.5.5.) After compilation, the tab, return, and linefeed characters are un-escaped and turned into whitespace: they remain intact, but you can no longer see directly what characters they are, which is a pity.

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

Linefeed

\\

ASCII character 92

Backslash

Don't confuse AppleScript's built-in string type and its native manipulations of this type with how scriptable applications may implement their own string behavior. When you ask an application to perform manipulations on text of its own, it may 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

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 dependent upon 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.4.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.4.2 String Elements

The following are the elements of a string. Bear in mind that you can't set them; you cannot alter 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 linefeed, or the one followed by the other (CRLF) as a line break.


text

A run of text. Its purpose is to let you obtain a single string using a range element specifier; see Section 10.7.6. 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 property of the AppleScript script object (the parent of top-level script—see Section 9.7.3) 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 only the first item of the list is effective.) That string is used to "split" a string into text items. The number of text items a string has is always exactly one more than the number of times it contains the text item delimiters string. For example:

set the text item delimiters to ":"
text items of "xxx:Users:mattneub"
-- {"xxx", "Users", "mattneub"}
set the 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. Since 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. Apple's documentation makes a big deal of this, but it's really no different from any of the other AppleScript properties, such as pi (see Chapter 16).

    [ Team LiB ] Previous Section Next Section