Monday, June 26, 2017

#1 2005-10-13 03:14:08 pm

julifos
Administrator
From: Malasa├▒a, Madrid
Registered: 2002-11-20
Posts: 2014

What is a "script object"?

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:

Applescript:

beep

Example 2:

Applescript:

script foo
   beep
end script

Example 3:

Applescript:

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):

Applescript:

name of foo
--> "foo"

script foo
   beep
end script

Or a parent (also a script object):

Applescript:

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):

Applescript:

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:

Applescript:

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:

Applescript:

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:

Applescript:

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:

Applescript:

beep

on beep
   continue beep 3
end beep

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.039 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)