*Very* strange script / global variable behavior
*Very* strange script / global variable behavior
- Subject: *Very* strange script / global variable behavior
- From: Neil Faiman <email@hidden>
- Date: Sun, 27 Feb 2005 14:04:29 -0500
I've been poring over Matt Neuberg's invaluable Definitive Guide, and I
thought I pretty much grasped the rules about the interactions of
scripts, handlers, variable scope, etc. Then I encountered this thing.
(I discovered this in a much bigger AppleScript Studio project, and
simplified it to this little reproducer, which demonstrates the problem
in Script Editor.)
property theState : 0
global theScript
script Outer
on initialize()
script Inner
on showTheState()
display dialog "State " & theState
end showTheState
end script
set theScript to Inner
end initialize
end script
set x to Outer
set theState to 1
tell Outer to initialize()
tell theScript to showTheState()
set theState to 2
tell theScript to showTheState()
If you run this, it will display "State 0" twice. I.e., it appears that
the Inner script's showTheState() handler is always displaying the
value of theState as of the time that the "set x to Outer" statement
was executed, rather than the value of theState at the time the handler
is executed (which I believe is the correct behavior), or the value at
the time the Inner script was actually instantiated by the call to the
Outer script's initialize() handler (which would probably be wrong, but
wouldn't surprise me).
If you comment out the "set x to Outer", then it will display "State 1"
and "State 2"!!
Alternatively, if you change "property theState : 0" to "global
theState", you will get "State 1" and "State 2". However, in the
original application that I reduced this from, the variable was a
global variable rather than a property, and I still got this behavior,
so that isn't a useful workaround.
Is this really a bug as it appears to be? (And if so, is there a good
workaround for it, other than not assigning the outer script to a
variable?) Or is it the way things are "supposed" to work; and if so,
can anyone provide the rule that would explain the behavior?
Thanks,
Neil Faiman
_______________________________________________
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