Re: Inheritance, Context, Scope, etc.
Re: Inheritance, Context, Scope, etc.
- Subject: Re: Inheritance, Context, Scope, etc.
- From: Paul Berkowitz <email@hidden>
- Date: Fri, 23 Jan 2004 17:16:38 -0800
On 1/23/04 2:40 PM, "Michael Terry" <email@hidden> wrote:
>
Unlike many other languages, AppleScript doesn't create space for a
>
variable when it's declared. AppleScript is dynamic; it doesn't have
>
any preconceived notions about the type or data size of a value that
>
will be held in the variable. Coupled with this fact, we can infer from
>
the behavior we've been discussing that uninitialized variables simply
>
don't exist, regardless of whether they've been declared.
This is very true. The one tricky, and rather hidden, tweak to this is that
once you've initialized a top-level variable - whether you've declared it as
global or not, as long as you haven't declared it explicitly as local - it
does persist after the script ends. So the second and all subsequent times
you run the script - unless and until you re-compile the script in a script
editor - that top-level variable does have a value and will no longer
exhibit the error you just pointed out.
So, save this as an application and double-click it a few times:
-------------------
script a
global b
end script
try
set a's b to 1
display dialog "Just set a's b to " & (a's b)
on error errMsg
global b
set b to 2
display dialog errMsg & return & "Just set top-level global b to 2"
end try
---------
The first time you run it, you get
--> "Can't set b to 1.
Just set top-level global b to 2"
After that you get
--> "Just set a's b to 1"
(You don't get the error version if you run it in a script editor. Only as
an application.)
--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.