Using Modules

How do I use a module?

A typical module will contain one or more handlers that you can call from your script. To send a command to a module, you must first refer to the property that contains the module. For example, after importing AppleMods’ Number module you can call its roundUp handler as follows:

_Number's roundUp(3.14)
--> 4

When should my script or applet load its modules?

Modules can be loaded either when a script is compiled (static binding) or each time it is run (dynamic binding).

Static bindingSimpler to implement: only requires a single line of boilerplate code, provided by LoaderWizard.Less flexible: if modules are modified or updated, compiled scripts need to be recompiled in order to see those changes.
Dynamic bindingMore flexible: modules are loaded each time the script is run, so any module changes or updates will automatically take effect the next time the script is run.Slightly more complex: the script’s main code is responsible for calling initScript once when the script is first run, so the script author must add this call themselves. This gets a bit fiddly in [e.g.] AppleScript droplets which have both run and open handlers - either of which may be called when the droplet is launched, and re-called multiple times if it’s a stay-open droplet as well.

Where can I find pre-written modules?

The AppleMods website provides a range of ready-to-use modules.

Alternatively, you can use any compiled AppleScript file as long as it’s named appropriately and placed in a suitable search domain.

How do I create my own modules?

Creating a new module is simple: just write a an AppleScript containing one or more handlers, and save it in a suitable format and location.

Loader can import any compiled AppleScript as long as its filename is structured according to a standard format and the script is placed in a suitable search domain.

How should I name my own modules?

The filename format recognized by Loader is as follows:

  • The module name. This consists of one or more alphanumeric characters (A-Z, a-z, 0-9), e.g. Date, MyFirstModule.
  • An optional version number. This consists of a hyphen (-) followed by three non-negative numbers separated by periods, e.g. 1.3.1, 0.11.0.
  • A .scpt or .scptd suffix. The .scpt suffix is used for single-file scripts; the .scptd suffix is used for script bundles. See AppleScript Editor’s built-in help for more information.

Here are some example filenames:


Can my modules load other modules?

Yes. Just add one or more properties to hold the loaded modules, followed by a suitable __load__ handler. LoaderWizard has an option for creating module loading code specifically for use in other modules.

Don’t use the AppleMods Loader and initScript commands in module code, as these features are only intended for use in main scripts, and using them in modules may also result in unexpected behaviour.