• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Global variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Global variables


  • Subject: Re: Global variables
  • From: deivy petrescu <email@hidden>
  • Date: Fri, 9 Mar 2007 17:57:17 -0500


On Mar 9, 2007, at 16:29, Stockly, Ed wrote:

deivy >>> First, to make the variable global declare it in the script main body. No need to declare it as global, it is global by default.
me>> That's not exactly correct.

deivy >>> what I wrote is correct.

Much of it was correct, but ...

No need to declare it as global, it is global by default.

This is the part that I believe is not exactly correct, particularly in the context of running the script as a droplet. If you declare a global in the top level, it is global to the script, but if you do not declare a global it is global only inside handlers where it has been declared, so there is a difference and I'd say there's at least two flavors of globals.



Ed,
First, I believe that may be semantics separates us. I mean to say the following: setting a variable in the top level script is (almost, I'll give you the two flavors of global) global by default. Setting a variable in a handler is local by default.
I give you that when you declare a variable at the top level as global, the variable is global throughout the script, including handlers. This is the example J. Stewart sent.
I concede that your definition of global is more appropriate than mine and then we both agree here.
I also agree that it is more convenient to use property. I rarely use global declaration.



Further, if you do not declare a variable in the top level of the script as a global and run the script as a droplet, then the top level variable does not become a global because the top level is not run. But a global declared in the top level is global to the droplet.

Now here we do not agree! Your script when run here as a droplet, gives me what I'd expect:
An error on the "open" handler.


The reason is, globals are set during the script run. Since the open does not run the script the global declaration does not take place.
Try property instead. It sticks because it is set at compilation time


<snip>

Deivy>>I run some tests with droplets here and declaring global or not, makes no difference, the variable will not be defined if it is set in the top level script

Below is a test I put together that I think illustrates a clear difference between using declared and undeclared gobals in the top level.


It can be run from script editor, or saved as an app and double clicked and run as a droplet.

Can anyone else corroborate that?
When I run the script below as a droplet I get an error on the open handler.
For what I understand Ed can run it with no errors as a droplet, which contradicts what I know and what I get.
Thanks



For what it's worth, I think there's a general consensus that it's better to use properties than globals.


If you want to use globals, I'd say declare them as globals in the top level of your script.

ES

global myTopLevelDeclaredGlobal
set myTopLevelDeclaredGlobal to "foo"
set myTopLevelUndeclaredGlobal to "bar"
display dialog myTopLevelDeclaredGlobal
display dialog myTopLevelUndeclaredGlobal

MyTestDeclaredGlobals()
MyTestUnDeclaredGlobals()


open ""
on open docList
display dialog myTopLevelDeclaredGlobal & " in Open handler"
try
display dialog myTopLevelUndeclaredGlobal & " in Open handler"
on error errText
display dialog "ERROR: " & errText & return & " undeclared in Open handler"
end try
MyTestDeclaredGlobals()
MyTestUnDeclaredGlobals()
end open


on MyTestDeclaredGlobals()
global myTopLevelUndeclaredGlobal
display dialog myTopLevelDeclaredGlobal & " in Delcared handler"
try
display dialog myTopLevelUndeclaredGlobal & " Delcared in handler"
on error errText
display dialog "ERROR: " & errText & myTopLevelUndeclaredGlobal & " ERROR Delcared in handler"
end try
end MyTestDeclaredGlobals


on MyTestUnDeclaredGlobals()
display dialog myTopLevelDeclaredGlobal & " undeclared in handler"
try
display dialog myTopLevelUndeclaredGlobal & " undeclared in handler"
on error errText
display dialog "ERROR: " & errText & return & " undeclared in handler"
end try
end MyTestUnDeclaredGlobals


Deivy

_______________________________________________
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
  • Follow-Ups:
    • Re: Global variables
      • From: Axel Luttgens <email@hidden>
    • Re: Global variables
      • From: Malcolm Fitzgerald <email@hidden>
References: 
 >RE: Global variables (From: "Stockly, Ed" <email@hidden>)

  • Prev by Date: Re: how to do a proper alphabetical sort?
  • Next by Date: Re: how to do a proper alphabetical sort?
  • Previous by thread: RE: Global variables
  • Next by thread: Re: Global variables
  • Index(es):
    • Date
    • Thread