Who has that property?
Who has that property?
- Subject: Who has that property?
- From: "Scott Babcock" <email@hidden>
- Date: Sun, 6 Nov 2005 18:22:23 -0800
- Thread-topic: Who has that property?
I work on a system that has several interdependent libraries, which
means that a library loaded by the main script can refer to another
library loaded by the main script. I use library reference properties
with standardized names to ease maintenance, which means that the main
script and the libraries contain properties with the same names. For
example
[ Library1 ]
----------------------------------------
| property lib2 : missing value
|
| on handler1()
| say "I'm handler 1 of library 1"
| my lib2's handler1()
| end handler1
----------------------------------------
[ Library2 ]
----------------------------------------
| on handler1()
| say "I'm handler 1 of library 2"
| end handler1
----------------------------------------
[ MainScript ]
----------------------------------------
| property lib1 : missing value
| property lib2 : missing value
|
| on run
| set my lib1 to load script alias "Mac OS X:Users:scoba:Library1"
| set my lib2 to load script alias "Mac OS X:Users:scoba:Library2"
| set lib2 of my lib1 to a reference to my lib2
| my lib2's handler1()
| my lib1's handler1()
| end run
----------------------------------------
Library2 doesn't have a lib2 property, but because of the scoping rules
of AppleScript properties, it's possible for MainScript to refer to lib2
of lib2. Looking into the Library2 script object, AppleScript discovers
that it has no lib2 property, so it checks the in parent of Library2 -
which is MainScript itself. So lib2 of lib2 is actually just lib2 of
MainScript.
The challenge is to detect this condition, since AppleScript is pretty
cagey about this sort of thing. Here's what I came up with to detect
inherited properties:
on whoHasLib2(theScript)
set scriptRef to a reference to lib2 of theScript
set parentRef to a reference to lib2 of parent of theScript
try
set parentVal to (contents of parentRef)
get parentVal
set inParent to true
on error
set inParent to false
end try
if (inParent) then
set scriptVal to (contents of scriptRef)
set (contents of scriptRef) to null
set inScript to not ((contents of parentRef) = null)
set (contents of scriptRef) to scriptVal
else
try
set scriptVal to (contents of scriptRef)
get scriptVal
set inScript to true
on error
set inScript to false
end try
end if
return {inScript, inParent}
end whoHasLib1
whoHasLib2(my lib1) --> {true, true}
whoHasLib2(my lib2) --> {false, true}
If the parent has the property, I determine if the script also has it by
writing to the script reference and reading from the parent reference.
If the parent value changes, I know that the script reference actually
refers to the property in the parent script.
Here's another interesting tidbit: While you can read from parentRef,
AppleScript complains if you try to write to it. This read-only behavior
may have practical applications, but I haven't explored this yet.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden