• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

AppleScript Language Basics

The example in the previous section only hints at AppleScript’s capabilities, but explaining every AppleScript command, object, and property would require a good-sized book all its own. This section summarizes AppleScript’s most common types of objects, commands, and related expressions:

  • Data Types: Data types are specific objects, each with their own properties. Common examples of data types are integers and real numbers on which you can perform mathematical operations; Boolean variables that can have logical values such as true and false; dates that have properties, such as month, day, year, and so on; string variables that can contain sequences of characters including spaces; lists that can contain sets of other data types; and many more.

  • Variables: Variables are instances of some specific data type that you create to separate the ideas in your program from any specific value that they’re operating on. In AppleScript, variables are explicitly created using the set command, but also can be created when they are referenced by an application. In the example program, you explicitly used the set command to create variables to hold the month and day of a date, while the repeat loop created a variable called eachitem, which stood for each item in the list of items that you dropped on the application’s icon.

  • Comments: If you’ve ever worked on a program written by someone else, you know that comments are your friend. AppleScript offers two ways to embed comments in your code. Enter single line comments by preceding them with two dashes, which tells AppleScript to ignore everything between the two dashes and the end of the current line. You can enter single or multiline comments by enclosing the comment within the (*” and “*) symbols.

  • Operators: Operators are mathematical or logical operations indicated by special symbols that can be performed on specific data types. AppleScript provides mathematical operators for real and integer variables, represented by symbols such as +,” “-,” “*,” “/, and so on. AppleScript also provides operators that work on sequences of characters, such as the & operator that concatenates two strings. It also provides logical operators such as the not operator for testing if something is not true, such as the value of a Boolean variable. Other logical operators are the and and or operators, which let the script react differently whether some, all, or any statements are true or false. AppleScript also provides the contains operator, which lets you determine whether one item is found in a list or whether a string contains a specific substring.

  • Flow-control statements: Flow-control statements enable you to execute only certain parts of your AppleScript programs based on the state of variables in your application or on your system. AppleScript commands such as if test whether something exists or is true, and react appropriately. Statements such as repeat until and repeat while let you perform some sequence of actions until a specified condition is met or while a specified condition is true.

  • Handlers: Handlers let you react to a specific class of Apple events, such as open, quit, run, and so on. The sample AppleScript application used the open handler to react to being told to open the files and folders dropped onto its icon.

  • GUI functions: AppleScript provides the display function to let you quickly and easily create various dialog boxes. These dialog boxes can contain specific messages, can contain customized buttons that you can bind to other actions in your AppleScripts, and can display standard Finder dialog box features such as pick lists.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint