A script object is the maximum (“biggest”) unit in AppleScript, which can contain any kind of event: statements, subroutine-calls, data storage and other script objects.
When you run a script, you are running a script object; for example, a compiled script running is itself an implicit script object.
You can load and declare “child” or “auxiliar” script objects within your script.
Example 1:
beep
Example 2:
script foo
beep
end script
Example 3:
set foo to load script file "path:to:script.scpt"
Examples 2 and 3 show static and dynamic (created at run-time) script objects, hosted within the main script object, which could be referred as me, the top-level entity by default, whose methods and properties can be accessed using possessive terms (me, my).
These script objects have some properties, such as name (also their variable name):
name of foo
--> "foo"
script foo
beep
end script
Or a parent (also a script object):
my parent
--> Ã?«script AppleScriptÃ?»
Using the parent property of script objects, you can share data between different script objects (the “child” inherits its parent’s stuff):
script foo
property y : 5
to test(x)
return x
end test
end script
property parent : foo
test(y) --> 5
In this simple demonstration of inheritance, the main script shares both the property “y” and the handler “test” of its new parent, “foo”. If you modify the property “y”, this is modified in both script objects, since it is the same variable:
script foo
property x : 5
end script
script foo2
property parent : foo
end script
set foo2's x to 2
foo's x --> 2
You can also declare an application as the parent for a script object and, consequently, inherit its properties and commands:
property parent : application "Finder"
using terms from application "Finder"
empty
name of first item
end using terms from
It does exist a special concept called delegation, as defined in the AppleScript Language Guide. If affects to “childs” whose parent has a handler called as one of its own. When you call such handler, the child’s one is executed, and you can also execute the parent’s one using a continue statement:
script foo
to doit()
display dialog "doIt from foo"
end doit
end script
script foo2
property parent : foo
to doit()
display dialog "doIt from foo2"
continue doit()
end doit
end script
foo2's doit()
There are some more uses of delegation within handlers, which you can use to redefine some events. We’ll drop a sample, but for further info concerning to the “continue” statement, take a look to the AppleScript Language Guide:
beep
on beep
continue beep 3
end beep