Re: global variables
Re: global variables
- Subject: Re: global variables
- From: Axel Luttgens <email@hidden>
- Date: Wed, 17 Jul 2013 23:06:48 +0200
Le 17 juil. 2013 à 22:27, Kaydell Leavitt a écrit :
> Hi Luther,
>
> On Jul 17, 2013, at 2:13 PM, Luther Fuller <email@hidden> wrote:
>
>> Global variables are persistent.
>>
>> In other words, when you launch an application, the values from the previous launch are remembered.
>>
>> This means that the application (bundle) is self modifying.
>>
>> Which, in turn, means that use of global variables will interfere with, for example, codesigning and perhaps other security measures used in Lion or Mt Lion.
>>
>> For this reason, I never use global variables and never modify a property.
>>
>> If any of this has changed recently perhaps someone can update us.
>
> I tried a global variable with the following code.
>
> global x
> set x to 2
> set x to x + 1
> display dialog "x: " & x
>
> It always displays 3 as the result.
Hello Kaydell,
This happens because you are resetting the variable to the same value again and again.
Try this one instead:
try
set x to x + 1
on error
set x to 2
end try
display dialog "x: " & x
> I don't know what you mean about global variables modifying their bundles.
The state of the script when, for example, saved as an applet, is saved in the executable.
Provided the executable is writable by the user running the script, of course.
> I have read that properties are more persistent than global variables,
Both globals and properties are persistent in exactly the same way.
> but I can't say that I understand properties, but I think that I understand global variables.
Those scoping matters are quite subtle (some would say "buggy") in AppleScript.
For example, the above script defines an "implicit global": no need to have a "global" declaration.
> I believe that global variables are defined for the entire script -- even inside of handlers.
Sometimes yes, sometimes not...
Your handler may come with a "local" declaration that hides the global.
But the global definition may also need to appear before the first use of an identifier in the script for having it recognized as the global entity, unless a global definition is used (which would probably not needed with an identifier associated with a top-level property).
And one may have scoping clashes, ending in globals and top-level properties to be undistinguishable.
> I thought that this was all that their is to global variables, having the scope of the entire script.
In fact, a global may have its existence extended throughout a whole AppleScript instance.
Hmmm... quite difficult to provide the gist of those AppleScript "subtleties" in a few sentences.
But many past discussions should be available in the list archives
HTH,
Axel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden