DekGenius.com
[ Team LiB ] Previous Section Next Section

9.2 Top-Level Entities

There are three kinds of top-level entity in a script object—properties, handlers, and script objects (see Chapter 7 and Chapter 8 for script properties and handlers).

9.2.1 Accessing Top-Level Entities

Outside a script object, its top-level entities are accessible on demand to any code that can see the script object. This means that they can be both fetched and set by such code.

The syntax is as follows:

  • Use the of operator (or the apostrophe-ess operator) to specify the top-level entity in relation to its script object. For example:

    script myScript
            property x : "Howdy"
            on sayHowdy(  )
                    display dialog x
            end sayHowdy
            script innerScript
                    display dialog x
            end script
    end script
    set x of myScript to "Hello"
    myScript's sayHowdy(  ) -- Hello
    run innerScript of myScript -- Hello
  • Alternatively, refer to the top-level entity within a tell block addressed to the script object. This requires the use of the keyword its, except in the case of a handler call. For example:

    script myScript
            property x : "Howdy"
            on sayHowdy(  )
                    display dialog x
            end sayHowdy
            script innerScript
                    display dialog x
            end script
    end script
    tell myScript
            set its x to "Hello"
            sayHowdy(  )
            run its innerScript
    end tell

In that last example, if you omit the keyword its from the line where you set x, you set an implicit global x, not the property of myScript. If you omit the keyword its from the line where you run innerScript, there is a runtime error, because no innerScript is in scope.

(It makes no difference whether or not the keyword its appears before the call to sayHowdy( ). This special treatment of handler calls is discussed under Section 8.5.1, and again later in this chapter.)

9.2.2 Persistence of Top-Level Entities

A top-level entity of a script object is persistent as long as you don't do something to reinitialize it. Typically, the top-level entity you're most immediately concerned with is a property. So, for example, the properties in this code persist and are incremented each time the script is run:

script myScript
        property x : 5
        script myInnerScript
                property x : 10
        end script
end script
tell myScript
        set its x to (its x) + 1
        tell its myInnerScript
                set its x to (its x) + 1
        end tell
end tell
display dialog myScript's x
display dialog myScript's myInnerScript's x

That code displays 6 and 11, then 7 and 12, and so forth, each time the script runs.

Script objects and handlers are top-level entities too, and they persist as well. Here's an example that illustrates this with a script object:

script myScript
        script myInnerScript
                display dialog "Hello"
        end script
        run myInnerScript
end script
script myNastyScript
        display dialog "Get lost"
end script
run myScript
set myScript's myInnerScript to myNastyScript

That code displays Hello the first time, but then it displays Get lost every time after that. The reason is that after the first time, myInnerScript has been replaced by myNastyScript, and this new version of myInnerScript persists.

As explained already under Section 7.6, if you edit a script and then run it, it is recompiled, so at that point persistence comes to an end: the script's top-level entities are reinitialized, and this includes its script objects, the script objects of those script objects, and so forth, along with all their top-level entities.

When a script object is defined within a handler, it has no persistence. The script object is created anew each time the handler runs.

    [ Team LiB ] Previous Section Next Section