12.3. Your Program Is a Script Object

You may not realize it, but all the programs you have worked with in this book have been script objects (which is why I've been referring to them as scripts). Think of your entire program as one big script object. You don't have to encapsulate it within script.....end script statements, but those statements are implicitly there.

Because the program itself is a script object, you can define properties at this top-most level. These properties persist through repeated executions of your program and even through repeated executions of your application! The property only gets reinitialized when the script is modified and recompiled.

12.3.1.

12.3.1.1. Try It Out: Defining Properties at the Top Level

In the following steps, you define properties at the top level.

  1. Type the following program into Script Editor:

    property X : 100
    
    set X to X + 1
  2. Click the Result tab and run the program. You get the following output in the Result pane:

    101
  3. Run the program again. You get the following output in the Result pane:

    102
  4. Run the program again. You get the following output in the Result pane:

    103
  5. Add a comment to the beginning of the program so that your program now looks like this:

    -- Initializing properties
    
    property X : 100
    
    set X to X + 1
  6. Run the program. You get the following output in the Result pane:

    101
  7. Run the program again. You get the following output in the Result pane:

    102
12.3.1.2. How It Works

The property X is initialized only once when the program ...

Get Beginning AppleScript® 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.