Rules for Naming AppleScript's Variables - dummies

Rules for Naming AppleScript’s Variables

By Tom Trinko

Because the Mac lacks common sense, there are rules that define what a variable can be called. Variable names

  • Can consist of the letters of the alphabet (both uppercase and lowercase), the numerals 0 through 9, and the underscore character ( _ )
  • Can’t have spaces
  • Can’t start with a number
  • Can’t be the same as reserved words

You get the underscore character by pressing the Shift+- (minus sign or dash).

In general, if you accidentally give a variable a name that is a reserved word, AppleScript gives you an error message. The bad news is that the error message doesn’t say something intelligible, like set is a reserved word. Instead, if you try and name a variable set and then try to compile the script, you get an error message (popping out of the top of your script window) telling you that a Syntax Error has occurred. The detailed error message says Expected expression but found “set”.

The good news is that once you get a little practice under your belt, avoiding reserved words is no big deal. For now, just tuck this particular fact into the sidebar space of your mind. Table 1 shows some examples of acceptable and unacceptable variable names.

Table 1: Sample Variable Names

Variable Name





Okay, but pretty hard to understand

this is a variable

Uh-uh: breaks rule # 2 (no spaces allowed)




Fine: You can mix and match uppercase and lowercase letters in a variable name




No way: % isn’t an allowed character


No, no, no! This is a reserved word

You don’t have to worry about mixing uppercase and lowercase letters in a variable name because AppleScript isn’t case sensitive. On the other hand, if you define two variables as The_Boss and the_boss, even though they look different to you, AppleScript treats them as being the same, which may cause problems in your script if you’re thinking that they’re different. The good news is that when you actually compile your script, Script Editor makes those two names look the same.

All right, you know the basics about what you can name variables. Here are some suggestions as to what style to use when picking variable names:

  • Pick names that mean something: While xxzs435 works just the same as The_Last_Cashed_Check_Number, you can more easily understand what’s going on in a script when you use comprehensible variable names.
  • Mark the start of words: You can use the underscore to separate words in a variable name, as in Check_amount. Other people like to use capital letters, as in CheckAmount. Neither is intrinsically better, but pick some scheme that works for you because you’ll find that a name like checkamount is harder to read as you quickly skim through a script than one that somehow marks the start of words.
  • Don’t worry too much about the rules: Sometimes you’re in a hurry, or you use a variable to hold a value for only a line or two in a script. In cases such as these, feel free to use a shorter variable name, such as x, y, or temp, instead of a longer but clearer name, such as width_of_window, height_of_window, or temporary_holding_spot, in order to save time and typing. Remember: Unless you’re doing this professionally — in which case you probably shouldn’t be wasting your time reading this — you’re the boss. No one but you has to be happy with your scripting style.tabmarktabmark

Now that you know how to name a variable, what do you do with one?