Re: parent properties - limited access inside child's handlers?
Re: parent properties - limited access inside child's handlers?
- Subject: Re: parent properties - limited access inside child's handlers?
- From: Nigel Garvey <email@hidden>
- Date: Sat, 3 Feb 2001 19:06:05 +0000
email@hidden wrote on Fri, 2 Feb 2001 23:42:37 EST:
>
I was fascinated to learn of the "parent" property, so I'm testing it. I
>
created a compiled script:
>
>
property a1: "hello"
>
>
and saved it to disk. I then created a script in script editor...
>
>
property parent : load script file "WorkZone:testparent"
>
display dialog a1
>
>
This works as expected - the dialog "hello" is displayed. I then created
>
this
>
script:
>
>
property parent : load script file "WorkZone:testparent"
>
>
my useparentprop()
>
>
on useparentprop()
>
display dialog a1
>
end useparentprop
>
>
This errors saying the variable a1 is not defined. If the properties and
>
handlers of the parent script are treated just as if they were inline with
>
the child script, then the useparentprop() handler should be aware of the
>
property a1.
>
>
Incidentally, the child's handler's are correctly aware of the parent's
>
handler's, just not the parent's properties.
>
>
Any ideas?
Apologies if this has been mentioned since the last digest was sent out.
There's a note on p.336 of ASLG for AS 1.3.7 which says:
"The previous example demonstrates an important point about inherited
properties: to refer to an inherited property from within a child script
object,
you must use the reserved word 'my' or 'of me' to indicate that the
value to which
you9re referring is a property of the current script object. (You can
also use the
words 'of parent' to indicate that the value is a property of the parent
script
object.) If you don9t, AppleScript assumes the value is a local variable."
In the example mentioned, the child script was accessing an inherited
property from within a non-inherited handler. So it seems that the
official way to access Jeff's 'a1' within 'useparentprop()' would be:
property parent : load script file "WorkZone:testparent"
my useparentprop()
on useparentprop()
display dialog my a1 -- NB. 'my'
end useparentprop
As Richard has commented, the properies of a parent script behave like
variables set in the top level of a child script; so you can also declare
'a1' as global within the handler, or in the top level - before or after
loading the parent:
property parent : load script file "WorkZone:testparent"
global a1
Conversely, you can also use 'my' to access a top-level variable in a
*self-contained* script:
set a1 to "Hello"
accessa1()
on accessa1()
display dialog my a1
end accessa1
NG