Install|Specific Functions of ModuleLoader

Working with AppleScrpt Libraries

About AppleScript Libaries

AppleScript Libraries is a library system for AppleScript which is built in OS X 10.9 or later. Script files plaeced in following locations are treated as libraries.

/Library/Script Libraries

~/Library/Script Libraries

Followings are sample codes to use a library.

use LibraryName : script "LibraryName"
LibraryName's sample_handler()
script "LibraryName"'s sample_handler()

In abobe scripts, a library "SampleLib" is loaded at run time, and a handler "sample_handler" defined in the libary is called.

The above is a minimum introduction of AppleScript Libraries. For details, see "Script Libraries" in "AppleScript Language Guide".

About ModuleLoader

ModuleLoader is an indpependent library system to "AppleScript Libraries". It is recommended to begin to use ModuleLoader as a helper. ModuleLoader can following functions can add to AppleScript Libray.

Regrettably ModuleLoadar can not handle libraries which have terminologies (SDEF files).

Loading Libraries at Compile-time

Consider loading a library with a use statement of "AppleScript Libraries" as follows. With normal "AppleScript Libraries", the library will be loaded when the script is executed. By using ModuleLoader, you can load the library at compile-time and make one script file including the libraries.

Add a property declaration and evaluate "script "ModuleLoader"'s setup(me)" in it below use statements to load libraries. script "ModuleLoader" will interpret use statements of a script object passed to "setup" handler and set a loaded library to the script object.

use scripting additions
use SimpleTextLib : script "SampleLibs/SimpleTextLib"
property _ : script "ModuleLoader"'s setup(me)

SimpleTextLib's replace_text("Do your task", "your", "my")
-- result : "Do my task"

If libraries are loaded into a script application when the script is compiled, the script can work on a computer in which the libraries and ModuleLoader.scptd are not installed. The script application will be good to distribute it.

Libraries can be stored in the Script Libraries folder of the bundle for the distribution. But it is not easy to find all required libraries without any exceptions. It is very easy to load libraries into properties at compile-time.

A limitation of ModuleLoader around working with "AppleScript Libraries" is that libraries with scripting terminologies(sdef files) can not be handled. Even if a library which has a terminology (sdef file) is loaded into a property, the library file must be placed in the Script Librareis folder to execute the command defiend in the sdef file.

Also ModuleLoader can not help treatments for libraries which have own bundle resources. Use statetments for threse libraries should be placed after "property _ : script "ModuleLoader"'s setup(me)" statements.

(* load at compile-time *)
use LibHasResource_before : script "SampleLibs/LibHasResource"
property _ : script "ModuleLoader"'s setup(me)

(* load at run-time *)
use LibHasResource_after : script "SampleLibs/LibHasResource"

log LibHasResource_after's text_in_resources() -- work
log LibHasResource_before's text_in_resources() -- error

Force to Reload Libaries

A Loaded library by "AppleScript Librareis" is cached by an AppleScript component instance which is managed by the application. Therefore in some cases, the library is not reloaded even if the library is changed on the file system. For example, a library loaded by a script on Script Editor is not reloaded untile the script is recompiled.

On the other hand, ModuleLoader allow you to control the timing of reloading libraries. The execution of "script "ModuleLoader"'s setup(me)" statement promise to load newest libraries on the file system.

use SimpleTextLib : script "SampleLibs/SimpleTextLib"

script "ModuleLoader"'s setup(me) -- force relaoding
SimpleTextLib's replace_text("Do your task", "your", "my")
-- result : "Do my task"
Install|Specific Functions of ModuleLoader