Naming Identifiers or Variables and Functions

The names that you create for variables and functions have to begin with a letter or underscore character ( _ ), but subsequent characters can include letters, numbers, and underscores. In variables or function names, you cannot include AppleScript’s reserved words and operators such as *, &, ^, or + (covered in Chapter 4) or special characters such as $, @, or #. An exception to this AppleScript rule allows for the creation of weird variable or function names if you use vertical bars (|) to begin and end the identifier, as in: set |2^$var| to 25. The variable |2^$var| is actually valid. If you wanted to create the equivalent of a Perl scalar variable in AppleScript, you could use: set |$perlVar| to 25. There is no practical limit to the size of AppleScript variable names; that is, you can have a variable name that has up to 251 characters, but you would never want to deal with variable names that long. In my OS 9 testing, a variable name that exceeded 251 characters produced the error dialog in Figure 1-10.

Script Editor signals a variable name that’s too long
Figure 1-10. Script Editor signals a variable name that’s too long

Get AppleScript in a Nutshell 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.