Re: Script Objects - help please!!
Re: Script Objects - help please!!
- Subject: Re: Script Objects - help please!!
- From: has <email@hidden>
- Date: Sun, 12 Jun 2005 16:04:37 +0100
Simon Forster wrote:
>Anyway, your hints got me to re-look at my library code and it transpires that I don't understand AppleScript's namespaces too well (and I thought I'd got this figured out).
>
>To ensure that the script objects's handlers were using (for both setting and getting) the script objects's properties, I was declaring these properties as globals within each handler - on the mistaken belief that the handlers would use the script objects's (global) properties.
global != property.
- A global's scope extends everywhere.
- A property's scope extends only as far as the script object it's declared in.
- A local's scope extends only as far as the handler it's declared in.
There are additional problems due to the braindead way the AppleScript compiler determines variable scope at compile-time, but as long as you follow some simple rules you won't run into these:
1. Declare all globals at the start of the main script (i.e. before any code that uses them).
2. Declare all properties at the start of the main script/script object (i.e. before any code that uses them).
3. If your code uses libraries then don't use globals in any of the scripts involved, only properties. (Globals are fundamentally evil, and can lead to obscure and undesireable interactions between supposedly separate libraries.)
>Anyone fancy taking on the task of explaining AppleScript's namespaces with particular reference to how they work with respect to script objects?
It'd be a lot easier to explain if AppleScript's variable scoping rules weren't such a mess to begin with. Unless you're going to be using inheritance, you can rely on AppleScript's lexical (compile-time) variable scoping plus the three rules above. Lexical scope is determined at compile-time: variable binding is determined according to the type of variable declaration (local/property/global) and its position in your script's containment hierarchy of nested handlers and script objects.
------- start main script -------
property x : 1 -- x is visible throughout main script
on makeFoo()
script Foo
property y : 2 -- y is visible throughout script Foo
on doSomething()
set z to 3 -- z is visible throughout handler doSomething
(*
In the following line:
x is lexically bound to property x in the top-level script
y is lexically bound to property y in script Foo
z is lexically bound to local z in handler doSomething
*)
return x + y * z
end doSomething
end script
end makeFoo
------- end main script -------
Dynamic scope is determined at runtime when 'my <variable>' (or '<variable> of me') invokes dymanic lookup of a variable, but follows different rules so I won't bother going into that unless you need it.
HTH
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
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