Re: *Very* strange script / global variable behavior
Re: *Very* strange script / global variable behavior
- Subject: Re: *Very* strange script / global variable behavior
- From: Paul Berkowitz <email@hidden>
- Date: Sun, 13 Mar 2005 09:04:20 -0800
On 3/13/05 8:01 AM, "Axel Luttgens" <email@hidden> wrote:
> By contrast, should someone have asked why following code errors,
> everybody would have said "hey! that's normal! you wouldn't want to be
> able to clash local variables that way, would you?":
>
> -- Code4
> set SomeProp to "xyz"
>
> script LogIt
> log SomeProp
> end script
>
> tell LogIt to run
> --> Error: variable SomeProp is undefined
>
> So, everyone would understand the error produced by Code4 because of
> basic and almost universal programming rules.
Hmmm? SomeProp is not a local variable. At the top level of a script, all
variables are global if not declared as local. True enough that an
undeclared variable is not seen as global _within its inner script objects_
unless it is identified as such there. This is pointed out by Matt. This
works:
--Code4A
set SomeProp to "xyz"
script LogIt
global SomeProp
log SomeProp
end script
tell LogIt to run
--> (*xyz*)
The rest of your discussion is excellent. There is quite a lot of "easy
behavior" in AppleScript that is illogical. As you say "AppleScript's
"handy" behaviors are then just adding their own layer of
confusion." Code2 and Code3 demonstrate that. These particular problems can
also be avoided by always declaring properties and globals at the very top
of a script or script object, never in the middle, even though AppleScript
allows the latter. Since most people do just that, we rarely run into these
problems.
--
Paul Berkowitz
_______________________________________________
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