Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: *Very* strange script / global variable behavior



Neil Faiman wrote on Sun, 27 Feb 2005 14:04:29 -0500:

>	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. ...
>
>If you comment out the "set x to Outer", then it will display "State 1" 
>and "State 2"!!

Script objects are definitions that are only initialised when the code 
containing them is run, so Outer only comes into being just before the 
'set x to Outer' line. Inner, on the other hand, isn't initialised until 
Outer's initialize() handler is run. It seems that setting another 
variable to Outer before this happens causes theState's value at the time 
to be set in stone as a literal value in Inner's definition rather than 
theState itself. One way to round this is to use a reference to theState 
rather than the actual property:

  script Inner
    on showTheState()
      display dialog "State " & (a reference to theState)
    end showTheState
  end script

... or:

  script Inner
    on showTheState()
      display dialog "State " & my theState
    end showTheState
  end script

I wouldn't like to say if this is a bug or an unfortunate combination of 
AppleScript subtlety and tortuous scripting.  ;-)

NG
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-users/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.