Re: global variable not being recognized by script object
Re: global variable not being recognized by script object
- Subject: Re: global variable not being recognized by script object
- From: Paul Skinner <email@hidden>
- Date: Thu, 01 Mar 2001 02:20:14 -0500
on 2/28/01 11:03 PM, Richard 23 wrote:
>
>> "my" also tells AppleScript to look outside of the current scope.
>
>> If a global is declared at the top level a single my reference
>
>> is sufficient (as long as the first occurence in the handler uses
>
>> my.
Inserted previous message
>
global foo
>
set foo to "bar"
>
>
TestMe()
>
--> "barbar"
>
>
on TestMe()
>
set bar to my foo
>
return foo & bar
>
end TestMe
>
>
If the global is defined anywhere other than the top-level, each
>
handler using a global needs to declare it global or use "my" to
>
reference it otherwise an undefined local is created.
mmmmm...
But am I correct thinking that here it was defined in the top-level, and
that this script works the same if you remove the 'my' construct. Well it
does here (PBbrz 400/OS 9.1/AS 1.5.5)
>
>
>
> Aha! You have just provided invaluable service to users of Smile, Richard.
>
> When calling entire handlers from a debugging Text window in Smile, you need
>
> to have put "my" before any property or global variable from the main script
>
> before "entering" the handler to use it in debugging. This is quite a
>
> bother. It is because it behaves exactly as Scott described: the variables
>
> in the "saved" (declared, what-have-you) handler don't know about any global
>
> variables or properties when it is "entered". But if you only need to put
>
> "my" once per variable, that's very convenient. I shall check it out.
>
hmmm...
>
Well yes, but I dunno how reliable that little trick is. I tend to not
>
use globals that often and when I do I specify any globals used by the
>
handler in its first line.
ummm..
>
Another method I'll use to avoid using "my" and "tell" concerns script
>
libraries. If I load in a script object and specify its parent as the
>
calling script I can get around having to direct calls at the child by
>
telling the child to call a handler in the parent. Any calls to the
>
child's handlers within the parent's handler can refer to the child's
>
handlers without an explicit reference. This is not true of script
>
properties but it is half of the battle!
>
>
R23
whaaa?
OK so I'm following, and then I get confused.
( ...=edited quotation )
>
Another method I'll use to avoid using "my" and "tell" concerns script
>
libraries. If I load in a script object and specify its parent as the
>
calling script...
I assume you mean that from within script "scriptA" you load a script
object "handlerB" in which you had previously coded a parent property that
identifies the specific script "scriptA" as it's parent. This would seem to
seriously impact handler reuse. Or did you mean that you assign the handler
"handlerB" a parent when you call it? Is that even possible?
>
... I can get around having to direct calls at the child by
>
telling the child to call a handler in the parent. ...
What?
You avoid having to direct calls to the child by telling it ? I don't
assume that you are using a 'tell handlerName' block around your handler
name. How did you mean this?
And then, did you mean:
that you (tell the child to call (a handler that is in the parent))
or
that you (tell the child to call a handler (from within the parent))
>
...Any calls to the
>
child's handlers within the parent's handler can refer to the child's
>
handlers without an explicit reference. ...
whew. That I got.
>
...This is not true of script
>
properties but it is half of the battle!
Are you saying that you cant refer to script properties of a child
script without using the 'my' construct?
I use parent properties in every script, every handler and in applets to
make all my handlers and properties directly addressable ( i.e. no 'my'
constructs, just the handler or property name ) in every script that I
write, so I thought I understood them.
Reading this explanation of how you use parent properties makes me
wonder, Do I have it all wrong and it works for me because of some bizzare
fluke, or do you really 'Think Different'. In fact, this prose is similar in
style to your scripts. I truely enjoy reading your posts here on this list.
They make me think very hard.
Like the crossword puzzles in my morning e-paper. 8 ^ )
--
Paul Skinner