Section 4.1. String Notation


4.1. String Notation

In AppleScript, pieces of text are stored in a format called strings. A string can contain as many letters, numbers, spaces, and punctuation marks as you'd like. In AppleScript, strings are typically placed between double quotes, like so:

"Yam farmer" "5504.45" "What the *#@&?!"

However, you won't get much use out of strings unless you can store them for later userather than having to constantly recreate them. To store a string, you use the set command, like this:

set welcomeMessage to "Take your shoes off before entering the building."

In the previous example, welcomeMessage is a variable: a name given to a stored value (Sidebar 4.1). Variables come in handy when you have a long string you want to recall later. Rather than having to type in all that text each and every time you want to use it, you just substitute the variable name anywhere you want the string to appear, like this:

set welcomeMessage to "Take your shoes off before entering the building." display dialog welcomeMessage

In the previous example, AppleScript notices the welcomeMessage variable that follows the display dialog command, and substitutes in the value that you set on the previous line. That's why running the previous script would display a dialog box that says "Take your shoes off before entering the building."

When using variables, it's important to remember that you must set a variable before you can access it later on in your script. If you switched the two lines in the previous script, for example, AppleScript would display an error, because you would be referring to a variable (with display dialog) before you told AppleScript what the variable should contain (with set).

AppleScript's set-before-you-access requirement leads many programmers to set all the variables that a script will use at the very beginning of the script. Under this arrangement, if you refer to a variable later in your script, you can be sure that it's already been defined at the script's start, thereby avoiding "variable not defined" errors.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net