DekGenius.com
[ Team LiB ] Previous Section Next Section

4.6 Applet and Droplet

An applet is a compiled script file format that functions as an executable. A dedicated editor such as the Script Editor offers the option to save a compiled script as an applet (or "application," depending on your Script Editor version). When you open an applet from the Finder, it launches as if it were an application and its code runs. Furthermore, an applet is scriptable. In short, an applet is a simple, easy way for someone who knows AppleScript to write a little application. This application has just about no user interface to speak of (unless some is added by way of a scripting addition). But the disadvantages of this can easily be outweighed by the simplicity of writing one.

An applet is an application. It is saved in Mac OS X with file extension .app, and in Mac OS 9 it has file type 'APPL' and creator 'aplt'. The Mac OS X Script Editor saves the applet with both features. There is also a new "application bundle" format, also with file extension .app, which is not backward-compatible with systems prior to Mac OS X 10.3 ("Panther").


A droplet is a form of applet; the difference is that a droplet does something when file or folder icons are dragged-and-dropped onto its icon in the Finder. Typically it then proceeds to process those files or folders in some way. Technically, a droplet is simply an applet whose script contains an open handler for dealing with the drop. Indeed, the very same script application can operate both as an applet (it does something when it is opened by launching) and as a droplet (it does something when files or folders are dragged-and-dropped onto its icon). A droplet has a different creator type from an applet ('dplt'), and it has a different icon, which looks like an applet's icon with the addition of a downward arrow. The Script Editor makes this distinction when you save the file, based on the presence or absence of an open handler.

Since opening an applet from the Finder launches it as an application, the applet needs to be opened in some other way in order to edit its script. For example, you can choose Open from the File menu within the Script Editor and select the applet, or drop the applet's icon onto Script Editor's icon. If the applet's script was not saved as run-only, the compiled script will then be decompiled and will be displayed for further editing, exactly as if this were just an ordinary compiled script file. A running applet may also display a menu item offering a chance to edit it. Thus, saving a compiled script as an applet does not prevent you from continuing to edit and develop the script—nor does it hide the script from prying eyes (for that, you must also save the script as run-only).

An applet contains a very small amount of genuine executable code—just enough to qualify it as a true application. This code, called the bootstrap code, is what initially runs when the applet is launched. It summons a scripting component called the Script Application Component. This component does the rest, handing the applet's compiled script over to the AppleScript scripting component for execution, and taking care of such things as putting up the applet's description window if there is one. The applet also contains the other resources necessary to make it a scriptable application.

In earlier versions of the Mac OS 9 Script Editor there was an option to save as a Mac OS X applet, but in my experiments a Mac OS X applet saved in this way wouldn't open successfully in Mac OS X. The most recent version of the Mac OS 9 Script Editor abolishes this distinction, and offers to save simply as an "application"; the resulting applet runs under either Mac OS X or Mac OS 9.

In earlier versions of the Mac OS X Script Editor (such as version 1.9), an applet could be set to "require Classic"; in this case the applet was saved as a Classic-only application and the Get Info option to toggle between opening in Mac OS X and opening in Classic was absent. An applet (but not an applet bundle) saved using the current Script Editor in Mac OS X can be launched in a previous system.

For further details about how to make and write applets and droplets, as well as to learn how to use AppleScript Studio to write more sophisticated applications with a user interface, see Chapter 24.

    [ Team LiB ] Previous Section Next Section