Loader Tutorial

This four-step tutorial shows how to load a module when the script is compiled and send it commands when the script is run. The module used in this exercise is Strftime, a powerful date-formatting tool included in the standard Loader distribution.

1. Create the module loading code

Launch the LoaderWizard applet provided with the Loader distribution.

At the main menu, select the 'Create main script header' option and click 'OK'. LoaderWizard will display a list of all installed modules. (If the Strftime module is not shown, check that it has been installed correctly according to the instructions provided in the Loader distribution.)

Select the Strftime module from the list and click 'OK'. LoaderWizard will generate the AppleScript code for loading this module at compile-time and place it on your clipboard.

2. Create the main script

Create a new script in AppleScript Editor. Paste the newly generated module loading code at the top of the script. You script should now look like this:

-- Module loading code (

property _Loader : AppleMods Loader -- Used to locate and load modules.

property _Strftime : missing value

on __load__(moduleLoader)
    tell moduleLoader
        set _Strftime to loadModule("Strftime")
    end tell
end __load__

property _ : _Loader's initScript(me) -- Bind modules at compile-time.


3. Add the main code

Add the code that will be executed when the script is run. For this exercise, we'll use StandardAdditions current date command to get a date object, convert this date to a string using Strftime's strftime command, and finally display the string in a new TextEdit document. Here is the code:

on run
	-- get the date
	set d to current date
	-- convert the date to a string
	tell _Strftime
		set s to strftime(d, "%Y-%m-%d %H:%M:%S")
	end tell
	-- display the string in a new TextEdit document
	tell application "TextEdit"
		make new document with properties {text:s}
	end tell
end run

4. Compile the script

Compile the script. In addition to compiling the rest of your code, the following will occur:

  • When this line is compiled:

    property _Loader : AppleMods Loader

    AppleScript will send the AppleMods Loader command to the AppleModsTools scripting addition to obtain a copy of the Loader module. The Loader module is then stored in the _Loader property for later use.

  • When this line is compiled:

    property _ : _Loader's initScript(me)

    AppleScript will send an initScript command to Loader, passing a copy of your script as its parameter. Loader will then initialize your script by calling its __load__ handler, allowing your script to load the Strftime module and store it in a property of its own, _Strftime. The Strftime module will then remain bound to your script until/unless it is recompiled.

Congratulations! You have just written your first Loader-based script. Run it to check that it works: it should create a new TextEdit document containing the current date and time formatted to look something like this:

2010-05-29 19:03:08


  • In step 3, notice how module commands, like application commands, need to be directed to a specific object. Application commands are sent to an 'application' object, while module commands are sent to a script object (the loaded module).

  • You can send commands to modules in one of the three following ways, depending on your needs and/or tastes:

    tell _Strftime
        set s to strftime(d, "%Y-%m-%d %H:%M:%S")
    end tell
    set s to strftime(d, "%Y-%m-%d %H:%M:%S") of _Strftime
    set s to _Strftime's strftime(d, "%Y-%m-%d %H:%M:%S")

    The first approach sends all commands within the tell _Strftime ... end tell block to the Strftime module. The other two approaches send only the strftime command itself to the Strftime module, so which one you use is purely a matter of personal taste.