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

Get AppleScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.