Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 5. Working with Strings - Pg. 125

5 Working with Strings In this chapter, you learn how to work with strings in more depth. You need to know how to manip- ulate strings to write more advanced programs. For example, you may want to add a suffix onto the end of every filename in a folder. Or you may want to see if each line in a document begins with a particular word. I show you more about how special considering and ignoring statements can be used for string comparisons. You also learn how to test whether strings begin with, contain, or end with a specified string. In this chapter, I also show you how to reference elements of a string, where an element can be a character, a word, or an entire paragraph. Understanding how to work with elements is critical to becoming proficient with AppleScript. The use of elements extends far beyond working with strings; you can apply elements to lists and to classes defined by other applications. Strings, Text, and Unicode Text Recall that a character string is simply one or more characters contained within a pair of double quotes. The quotes define the beginning and end of the string, but they do not actually get stored as part of the string. So when you write a statement like this: set message to "Good afternoon!" just the 15 characters between the quotes get stored inside message , and not the quotes themselves. You can make your strings as long as you like. When entering long character strings, you can let Script Editor wrap the string to the next line for you. If you break the string by entering the line continuation character Option-L (or Option-Return in Script Editor) or by simply pressing the Return key, that character becomes part of the string, which may not be your intention.