Variables

Here are two ways that you can create your own variables in AppleScript:

set int to 20 -- one way to set a variable to an integer

copy 20 to int -- another way

A variable is a word or identifier that the scripter creates to store a script value. An example is the int variable in the statement set int to 20. Along with copy, the set reserved word is used to set a variable name to a value, in this case an integer. AppleScript variables can store any value, including booleans, lists, numbers, records, strings, and application-defined classes. AppleScript variables have to begin with a letter or underscore ( _ ) character, but subsequent characters can include numbers and underscores. You cannot include operators and other symbols that AppleScript reserves for different uses (such as *, &, ^, or +) or special characters (such as $, @, or #). An exception to this rule in AppleScript allows the creation of memorable variable names if you use vertical-bar characters (|) to begin and end the identifier:

set |2$var*&^%#| to 2

AppleScript is not a case-sensitive language, so the variables that include the same characters but in varying case are treated as the same identifier. In other words, myname, myName, and MYNAME are all considered the same variable. Variable names can be one to several characters long, depending on your stylistic preferences.

This AppleScript gives several examples of valid and invalid variable names, as well as how to use the set and copy keywords to ...

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.