Global Variables

I'm writing a project in applescript studio, and I can't seem to grasp how global variables work. In my main script, I have some code that sets a few global variables. It's all great, until I try to access them from within another script. The main script is here:
on launched theObject
set prefPaneNibLoaded to false
global prefsLocation
global prefsFile
global monsters
global animals
global maxplayers
global pvp
global spawnprotection
set prefsLocation to ""
set prefsFile to ""
set monsters to ""
set animals to ""
set maxplayers to ""
set pvp to ""
set spawnprotection to ""
end launched
and the startup one is here
on clicked theObject
tell button "prefAnimals" of window 1
if animals is "true" then
set animals to "false"
else
set animals to "true"
end if
end tell
end clicked


These are in different windows, called by different objects. Anything i'm doing wrong? It just gives me a
The variable animals is not defined. (-2753)

2007 iMac, Mac OS X (10.5.8)

Posted on Mar 12, 2011 8:27 AM

Reply
7 replies

Mar 12, 2011 10:25 AM in response to arilotter

Your global declarations are inside a handler. Using a local or global declaration inside a handler essentially tells AppleScript which variable(s) to reference in the event you are using the same name. In this case, since the variables are not defined at a global level elsewhere, they are essentially local (I think I said that right).

Just move your initial global declarations outside of any handlers (like you would a property declaration).

See the AppleScript Language Guide or this MacScripter article for more information about variable scope.

Mar 13, 2011 11:22 AM in response to arilotter

Not sure what you mean by "it never gets called", but where the global declaration is made also determines its scope. If the globals are declared at the outer level of your script, then they will be valid for the entire script.

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 340px;
color: #000000;
background-color: #FFD891;
overflow: auto;"
title="this text can be pasted into the Script Editor">
global prefsLocation
global prefsFile
global monsters
global animals
global maxplayers
global pvp
global spawnprotection


on launched theObject
set prefPaneNibLoaded to false
set prefsLocation to ""
set prefsFile to ""
set monsters to ""
set animals to ""
set maxplayers to ""
set pvp to ""
set spawnprotection to ""
end launched


on clicked theObject
tell button "prefAnimals" of window 1
if animals is "true" then
set animals to "false"
else
set animals to "true"
end if
end tell
end clicked</pre>

Mar 13, 2011 12:25 PM in response to arilotter

Defining a variable as global (or local) just determines its scope. Normally, when you use a variable inside a handler, it is local to that handler only and goes away when that handler finishes. In order to use a variable across different handlers, you can declare them as global, in which case the scope of the variable can extend to the top level of a script object (depending on where it is declared).

Global variables are not retained between script runs, and from the AppleScript Studio Programming Guide, "global variables declared in one script are not accessible from another script without doing an explicit *load script* command." Depending on what you are doing, you might take a look at passing just the desired parameter(s) to handlers in your other script object, or use properties or the *user defaults* class.

I wrote a script a while back that scans various initialization files to generate a command line for an Unreal Tournament server, but I didn't use separate script objects (or even AppleScript Studio) - is this project a modification of something existing?

Mar 13, 2011 1:48 PM in response to arilotter

Properties are a little bit different from globals, in that the global declaration more or less just defines the scope (no value is set), while a property declaration cannot be in a handler and includes an initial value. I'm not sure how to access the external script in AppleScript Studio without using load script (I am running AppleScriptObjC, and can't tell how much they are mixed), but once the script is loaded you can access a property in much the same way as you access its handlers, e.g. get Bukkiteer's monsters.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Global Variables

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.