Global variables that aren't...

Why can’t I share a global variable between two scripts in the same project?

In my main script:


global isSaved

on will finish launching theObject
	set isSaved to false
end will finish launching

In another script (split out primarily to reduce clutter in an increasingly long and complicated script):


global isSaved --including or excluding this line makes no difference

on choose menu item theObject
	set objName to name of theObject
	if objName is "menu_new" then
		if isSaved is false then display dialog "File not saved"
	end if
end choose menu item

If I reference “isSaved” in the main script, it works fine. But in the other script (which holds all my menu actions) it just tells me that the variable is not defined.

Anyone have a fix for this? I could just suck it up an combine the scripts into a single script but it seems silly for Apple to provide a way to divide your code up but not let those pieces exchange data. Oy…

Thanks!!

Hi Steven,

globals and properties are valid only for the script, they were defined in.
Script objects are your friend, take a look at the sample project Unit Converter in /Developer/Examples/AppleScript Studio/

While this method seems far more complicated than it should be, I’ll give it a shot. Why can’t it be as easy as C/C++'s #include? :wink:

Thanks!