Variables
To complete our map of the world of a script, we must include variables. A variable is an association (formally, a binding) between a name and a value. You can think of it as a shoebox with a label on it, into which something is placed for storage. The shoebox's label is the variable's name; what's inside the shoebox is the variable's value. For example, when we say:
set x to 5
it is as if we had a shoebox labeled "x" into which we place the number 5.
Naturally, variables are very useful things, which is why just about all computer languages have them. To be able to assign your own names to things makes your program clearer and easier to maintain. And in a way any computer program is all about manipulating values, so it's nice to have a place to put each value when you're not using it, so that you can retrieve it again later. A variable's value is like the coffee in your cup when you set the cup on the table, do something else, and then pick up the cup and take a sip. The cup is like the variable; without it, the coffee would just flow onto the floor when you're not using it.
Variables and code are intimately related. Code is where variables are defined; code is where variables are given their values and where those values are retrieved.
Variables and scope blocks (handler definitions and script object definitions) are intimately related. Variables are what scope is all about: they are the "things" that other regions of the code either can or can't see, depending on the rules ...
Get AppleScript: The Definitive Guide, 2nd Edition 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.