Re: Script Objects - help please!!
Re: Script Objects - help please!!
- Subject: Re: Script Objects - help please!!
- From: Simon Forster <email@hidden>
- Date: Sun, 12 Jun 2005 20:25:55 +0100
On 12 Jun 2005, at 16:04, has wrote:
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.
Thank you. Clear and concise as ever.
Question: To make sure that the handler references its "closest"
variable (in this instance I'm talking about properties declared in
the script object), I've used the "x of me" grammar - which _seems_
to be working fine. According to your rules, this isn't necessary -
and you hint at unexpected consequences. Is this likely to come back
and bite me? Should I just go through my library script and delete
all these unnecessary "of me"s? (A bit of a shot in the dark for you
without seeing the entire context but I'd like to hear your opinion).
Thanks for your time & help here - it really is appreciated.
Simon Forster
_____________________________________________________
LDML Ltd, 62 Pall Mall, London, SW1Y 5HZ, UK
Tel: +44 (0)70 9230 5244 Fax: +44 (0)70 9230 5247
_____________________________________________________
_______________________________________________
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