Load script from other scripts


Edit: I just realized that I should say “Handlers” instead of “Functions”. (Old habits, you know.)

I’ve built a multi-script application. A couple of functions are used in one or more other scripts. Currently I copy the functions in every script as I can’t get the “load script” function to work.
(Please note that I’ve tried to read every post about this subject. Hence the several ways of trying to achieve this)

I built a Functions.applescript. Inside this applescript, I have (for example)

on FUNC_Initialize_BP_PGB(titlebarmsg, topmsg, bottommsg)
        tell window "ProgressWindow"
                set title to titlebarmsg
                tell text field "TopMsg" to set content to topmsg
                tell text field "BottomMsg" to set content to bottommsg
                tell progress indicator "BarberPole"
                        -- This makes the application use a separate thread to handle the animation
                        set uses threaded animation to true
                        set indeterminate to true
                        set visible to true
                end tell
        end tell
end FUNC_Initialize_BP_PGB
on FUNC_quit_BP_PGB()
        tell window "ProgressWindow"
                tell progress indicator "BarberPole"
                end tell
        end tell

end FUNC_quit_BP_PGB
more functions

These functions work fine as long as I use a separate copy in each applescript.

However, to use this Functions script as a reusable functions library (as I’m used to in other languages) I tried in my other scripts, that use these common functions, to:

on will finish launching theObject
        global mYFunctions -- Needed to set this one otherwise I got an "unknown variable" error
        set mYFunctions to load script (path to resource "Functions.scpt" in directory "Scripts")
        set FUNC_Initialize_BP_PGB to FUNC_Initialize_BP_PGB()
        set FUNC_quit_BP_PGB to FUNC_quit_BP_PGB()
        ... some more functions ...
end will finish launching

In the other scripts I tried then all kind of options like:

FUNC_Initialize_BP_PGB(titlebarmsg, topmsg, bottommsg)  -- direct call based on reference
tell mYFunctions to FUNC_Initialize_BP_PGB(titlebarmsg, topmsg, bottommsg)
mYFunctions's FUNC_Initialize_BP_PGB(titlebarmsg, topmsg, bottommsg)

None of them works and throws an error in my face during startup.

What am I doing wrong?

I just tried (based on http://bbs.macscripter.net/viewtopic.php?id=24759):

property mYFunctions : load script (path to resource "Functions.scpt" in directory "Scripts")

to use that later as:

set PB to mYFunctions's FUNC_Initialize_BP_PGB(titlebarmsg, topmsg, bottommsg)

But I can’t even set the property. I can’t save the applescript so obvious something is wrong in the syntax of the property statement.
I also tried to set the property “as alias” and “as text” without result

I have a pretty complex app that has multiple script items inside it that get called at times, like you are describing.

I think your property statement won’t compile because the compiler often looks for the actual file you are declaring. But since the app isn’t built yet, it can’t resolve it. You should use a more variable based approach to declare the property.

This is how I have it. Sorry it’s a little complex but I am just copy/pasting from my app so I don’t have to rewrite it:

on load_a_Script(scriptName)
return load script file (my pathToScripts() & scriptName & “.scpt”)
–read a file at this location inside me, the application
end load_a_Script

on pathToScripts()
set appPath to (path to me from user domain) as text
return (appPath & “Contents:Resources:Scripts:”) as text
–get path to me and this script files folder
end pathToScripts

on load_Scripts()
set overprint_script to my load_a_Script(“BoxOverprintFill”)
end load_Scripts

on will finish launching theObject
my load_Scripts()
end will finish launching

will finish launching is called on app startup
calls load_Scripts() script
load_Scripts() calls load_a_Script to try to load a script with the name “BoxOverprintFill.applescript” saved in a folder I called “Scripts” inside of the app resources. “BoxOverprintFill.applescript” is turned into “BoxOverprintFill.scpt” when you compile and build the app.
the script load_a_script is also calling the script “pathToScripts” which returns the location of the scripts at any given time. Really it returns something like “Macintosh HD:Applications:MyGreatApp:Contents:Resources:Scripts:”
the load_a_script then concatenates the file location and the name of the script together to give the location of the script file.

I also had to set some globals for other stuff in the app to actually call or run the individual scripts.
This is probably not be the best or shortest way to do it. But it works in my app.
You may also look at Apple example files, I believe I learned this from looking at the “Unit Converter” example a lot, it loads script files.

Thanks for your reply SuperMacGuy and sorry for replying this late. I’ve been ill for over a week and I’ve read your reply but didn’t have the energy or enthousiasm to work on it.

After some very frustrating hours using the “on launched” and “on will finish launching” blocks (which are used in the Unit converter, some other Apple sources and your example) and changing my scripts back & forth I finally discovered that it only works in my (three) scripts calling the “functions lib” (handler lib) if I place the initial “my load_Scripts()” in an “on awake from nib theObject” in all three “calling” scripts.

So right now I’m happy (oh, I’m so happy), but can anyone explain this to me as I really don’t understand why it doesn’t work according the documentation?
(I feel like a monkey on a non-intelligent trial&error path finally getting it to work but not understanding why).

I did RTFM but I now feel like FTM (F*** the Manual)

Again: thanks anyway for your time and effort to help me. It certainly did help.