This is driving me crazy and is SO simple to do, and none of the examples here or elsewhere seem to work, can someone help please?
I’d like to be able to separate my code into different libraries, and at launch, load the libraries and then call the libraries from other libraries.
in my main application script I’d like to…
set utilities to load script POSIX file (path for script “Utilities” extension “scpt”)
then in another script simply
tell utilities
set x to doSomething(y)
end tell
… this is SO simple and I always either get an error that says the script doesn’t understand that message, or it seems the script hasn’t loaded properly…
global utility_lib
property utility_lib : missing value
property inited : false
on init_me()
try
set utility_lib to load script (((main bundle's scripts path) & "/utility_lib.scpt") as POSIX file)
set x to utility_lib's doSomething(y)
on error the_error
log the_error
return false
end try
set inited to true
return true
end init_me
on will finish launching the_object
if not inited then if not my init_me() then quit
-- the rest of the WFL routine here...
end will finish launching
on awake from nib the_object
if not inited then if not my init_me() then quit
-- the rest of the AFN routine here...
end awake from nib
on launched the_object
if not inited then if not my init_me() then quit
-- the rest of the L routine here...
end launched
I use a quite simple approach, which I copied from the Apple site, I believe, and it works fine.
The first script loads the shared script(s) into globals
global HandleMail
global HandleFiles
on activated SomeApp_app
my loadScripts()
end activated
on pathToScripts()
set appPath to (path to me from user domain) as text
return (appPath & "Resources:Scripts:") as text
end pathToScripts
on loadScript(scriptName)
return load script file (my pathToScripts() & scriptName & ".scpt")
end loadScript
on loadScripts()
set HandleMail to my loadScript("HandleMail")
set HandleFiles to my loadScript("HandleFiles")
end loadScripts
Then in all other scripts in the same project you can use those globals, provided you declare them first:
global HandleMail
global HandleFiles
on myFunction()
tell HandleMail to getNewMail()
tell HandleFiles
makeNewFile()
deleteOldFiles()-
end tell
end myFunction