Team LiB   Previous Section   Next Section
string

Allowed coercions

list with one item; numbers as long as the string is a valid integer
real
international text
Unicode text

Syntax

set myString to "Good old string" as string

Description

AppleScript strings are like strings in other languages (arrays of characters), except that they have to be surrounded by double quotation marks; you do not have the option of using single quotation marks. You can get the number of characters in a string, including spaces, by using a string's length property:

length of myString

AppleScript strings have the following built-in elements:

characters
paragraphs
text
words

Examples

The statement:

words of myString

or:

every word of myString

will return a list of strings containing each of the words in the original string ({"Good","old","string"}). This is handy if your script wants to examine or otherwise process each of the words in a string. The same goes for the other elements; characters of myString returns a list of strings, with each item in the list being a single-character string ({"G", "o", "o", "d", " ", "o", "l", "d", " ", "s", "t", "r", "i", "n", "g"}). The following example iterates through this list and returns the number of non-space characters in the string. Items in lists are one-based, meaning that the first character in a string is character 1. These statements would make a nice subroutine for a string-handling library:

set myString to "Good old string"

set noSpaceCharCount to 0

repeat with c from 1 to (length of myString)

(*space is a string constant representing a space character *)

   if not (character c of myString is space) then 

      set noSpaceCharCount to noSpaceCharCount + 1

   end if

end repeat

get noSpaceCharCount

The text element of a string allows you to grab ranges of characters in a string and return the value as a string rather than a list:

characters of myString

or:

characters 2 thru 8 of myString

returns a value of type list. For example:

get text 6 thru 15 of myString

would return "old string."

Use a backslash or escape character ("\") to produce double quotation marks, tabs, or returns in strings.

You might want to use escape characters in the window produced by the display dialog scripting addition command. The following example shows how to use the escape character in an AppleScript string value. If you then write the string to a file, for instance, the escape character ("\") will not appear in the written out string, just the characters that it "escaped," such as double quotation marks:

set myString to "\"Two words \t \ttwo tabs and \ra return character\"" 

 (* returns "\"Two words   two tabs and 

a return character " *)

The AppleScript concatenation character (&) can connect two strings. For example:

"String one " & "String two"

results in "String one String two." Make sure to include spaces in the connected strings to ensure readability. This is an operator that you will be using with strings all the time.

A string can be coerced to a number, and vice versa, as long as the string looks like a number. You can convert "55" or "3.14" to a number, but you cannot coerce "1.3.7" or "1.4Stephanie" to a real or integer. You can coerce and then perform math on a string that is a valid number, for instance. This code finds out if a user has AppleScript version 1.4 or greater on their machine:

set ASversion to version as string

if ((text 1 thru 3 of ASversion) as real)  1.4 then (* string coerced to 

real *)

   display dialog "good, you're running at least AppleScript 1.4"

else

   display dialog ("Maybe you should consider upgrading to AppleScript¬ 

   1.4 or 1.5;You" & " are now running " & ASversion)

end if

If you poke around in the various scripting additions, you will find many that work with strings. For example, as part of the Standard Additions, the offset osax will find the position of one string inside of another (Appendix A is devoted to scripting additions):

set theString to¬

"Robert Cohn was once middle-weight champion of Princeton." 

offset of "Princeton" in theString 

(*returns the character position where 'Princeton' begins which is the integer 48 *)

    Team LiB   Previous Section   Next Section