Handlers from a "load script" inheriting global variables?

I read the Variable Scope FAQ, and I’m able to have handlers use and modify global variables quite nicely.
…but I’m having trouble getting handlers from another loaded script file to do the same.

Main script:

global thepath

on clicked theObject
	set snippets to load script file (POSIX file (thePath & "/Contents/Resources/Scripts/snippets.scpt")
	test() of snippets
end clicked

on starting()
set thePath to path of the main bundle as string
end starting

Outside script (snippets.scpt):

on test()
	display dialog thePath
end test

I get an error while running my app that “The variable thePath is not defined.”.
The test() handler from snippets.scpt is not picking up the global thePath variable from the main script.

Basically, what I want to do is make dozens of “code snippets” in multiple .scpt files that I can call as if they were just copy and pasted into the main script - thus preventing clutter and allowing code reuse.

Is what I want to do completely wrong and/or impossible? Is there a little something more needed to make my global variables “globaler”?


Try declaring the variable as global in the loaded script.

global thePath

on test()
display dialog thePath
end test

Personally, I would just pass the variable to the subroutine.

on test(t)
display dialog t
end test

and call it like this.

test(thePath) of snippets


That did it! Thanks.
For…“architectural” reasons, I didn’t want to go passing variables every time I called these handlers.
I’ll now be adding about 60 global variables to a bunch of .scpt files.
Hey, it’s better than the alternatives :stuck_out_tongue: