RE: loaded script subroutines
RE: loaded script subroutines
- Subject: RE: loaded script subroutines
- From: "Scott Norton" <email@hidden>
- Date: Wed, 14 Jan 2004 22:13:20 -0500
- Thread-topic: applescript-users digest, Vol 3 #2331 - 16 msgs
On Tue, 13 Jan 2004 15:47:11 -0600, "Wallace, William" <email@hidden> asked,
>
I have a script which loads several other scripts containing handlers for various
>
tasks. When I test these scripts in isolation from each other they work fine. But
>
when they're all loaded into the same script, there are times when they need to call
>
each other and then the errors start flying (usually a -2753 the variable <yada yada>
>
is not defined). For instance:
>
>
--begin main script
>
set scriptVar1 to load script "path:to:script1"
>
set scriptVar2 to load script "path:to:script2"
>
set theData "some string or something"
>
>
tell scriptVar1 to doStuff(theData)
>
--end main script
>
>
--Script1
>
on doStuff(myData)
>
display dialog myData giving up after 10
>
tell scriptvar2 to doMoreStuff(myData)
>
end doStuff
>
--end script1
>
>
--Script2
>
onDoMoreStuff(myArgs)
>
set myString to myArgs & " meaningless"
>
display dialog myString
>
end doMoreStuff
The problem is a matter of scope and visibility. scriptVar1 and scriptVar2 are local variables, and not visible to the loaded scripts. (This is a good thing because it keeps the loaded scripts from inadvertantly stepping on the variables in the scripts that load them.)
The best way to fix this would be to keep the things that work together in the same script, and let AppleScript's modularity features keep things that don't work together from bumping into each other.
So, if script 2 is an auxilary function for script 1, put script 2 in script 1, not in the main script. For example,
--begin main script
set scriptVar1 to load script "path:to:script1"
set theData "some string or something"
tell scriptVar1 to doStuff(theData)
--end main script
--Script1
on doStuff(myData)
set scriptVar2 to load script "path:to:script2"
display dialog myData giving up after 10
tell scriptvar2 to doMoreStuff(myData)
end doStuff
--end script1
--Script2
onDoMoreStuff(myArgs)
set myString to myArgs & " meaningless"
display dialog myString
end doMoreStuff
Personally, I like to load scripts at compile time, if they are static modules as they usually are:
--begin main script
property scriptVar1 : load script "path:to:script1"
set theData "some string or something"
tell scriptVar1 to doStuff(theData)
--end main script
--Script1
property scriptVar2 : load script "path:to:script2"
on doStuff(myData)
display dialog myData giving up after 10
tell scriptvar2 to doMoreStuff(myData)
end doStuff
--end script1
--Script2
onDoMoreStuff(myArgs)
set myString to myArgs & " meaningless"
display dialog myString
end doMoreStuff
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.