all about globals

tell me everything u know about global variable
all i know is how to declare them

global thevar

i dont know how to set them of how they work or how to use them

Search our FAQ for “global”:
http://www.macscripter.net/faq/

Well, they are set like any other variable:


    global gVariable

    set gVariable to "my global variable"

I have always found AppleScript’s rules of “variable scope” to be rediculously complicated. They are explained in full in the AppleScript Language Guide, but after 8 years of AppleScripting, I still get lost in trying to work out exactly where AppleScript goes looking for identifiers in the name space:

http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/index.html

In particular:

http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScript.eb.html

It’s a lot easier in languages that have simple rules, like:

  • all variables are local, unless explicitely made otherwise
  • all variables are global, unless explicitly made otherwise
    etc…

I’ve learned to avoid globals altogether, not only because they tend to encourage bad programming practice, but also because AppleScript just makes their scoping to complicated to follow. Consider these charts:

Figure 8-1
Scope of property and variable declarations at the top level of a script

Figure 8-2
Scope of property and variable declarations at the top level of a script object

Figure 8-3
Scope of variable declarations within a handler