[ Team LiB ] |
13.4 StringA 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.
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 PropertiesThe following are the properties of a string. They are read-only.
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 ElementsThe 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.
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 ] |