ModuleLoader Reference|Not Frequently Asked Questions

Examples

Loading Libraries at Compile-Time

If libraries are loaded at compile-time and set in properties of a top-level script, libraries are embedded in the top-level script. The script does not require libraries and ModuleLoader at run-time.

The script is good to copy to other computers or distribute it. On the other hand, libraries are not updated until recompiling the top-level script. If you want to use newest libraries always, see the next section "Loading Libraries at Runtime".

use SimpleTextLib : script "SampleLibs/SimpleTextLib"
-- property SimpleTextLib : "@module" -- same meaning to the above
property _ : script "ModuleLoader"'s setup(me) -- load library at compile-time.

SimpleTextLib's replace_text("How now brown cow", space, "-")
-- result : How-now-brown-cow

Loading Libraries at Run-time

A benefit of loading libraries at run-time is that newest libraries can be used always. It is recommended to follow AppleScript Libraries way.

In the case you must use ModuleLoader, evaluate "script "ModuleLoader"'s setup(me)" statement not in a property definition but in run handler or open handler as follows

property SimpleTextLib : "@module"

script "ModuleLoader"'s setup(me) -- load library at runtime.
SimpleTextLib's replace_text("How now brown cow", space, "-")
-- result : How-now-brown-cow

Loading Libraries into Local Variables

tell script "ModuleLoader"
set SimpleTextLib to load("SimpleTextLib")
end tell

tell SimpleTextLib
-- do somehting
end tell

Use Libraries in a Library

Consider you want to use other libraries from a library. Just like the top level script, prepare properties which evaluate module commands. When the library is loaded, ModuleLoader will set libraries to the properties which evaluate module commands.

property name : "SimpleListLib"
property SimpleTextLib : "@module"

on replace_text_in_list(a_list, target, replacement)
set new_list to {}
repeat with a_text in a_list
tell SimpleTextLib
set end of new_list to replace_text(a_text, target, replacement)
end tell
end repeat
return new_list
end replace_text_in_list

on run -- test code
script "ModuleLoader"'s setup(me)
replace_text_in_list({"cain", "cine"}, "c", "p")
end run

For tests of the library, evaluate "script "ModuleLoader"'s setup(me)" at run-time.

Loading Libraries in a Bundle

Following example shows loading libraries stored in an application bundles. It is assumed that libraries are placed under "Resources/Script Libraries" folder.

property LibInBundle : "@module"

tell script "ModuleLoader"
prepend_path(path to resource "Script Libraries")
setup(me)
end tell

run LibInBundle -- "I'm LibInBundle"

Add the location in the bundle where libraries are located with "prepend_path" handler of a ModuleLoader script. And then call setup command to load and set libraries.

This is not recommended way. It is recommended to embed all libraries into properties at compile-time or to use built-in "AppleScript Libraries" introduced in OS X 10.9.

Above sample shows flexibility of ModuleLoader.

ModuleLoader Reference|Not Frequently Asked Questions