Applets and Droplets

It is possible to save an AppleScript script as an executable file or applet.

If the script has a run handler:

to run
        -- do stuff
end run

then this will be invoked when the user double-clicks the applet. (If there are no handlers in the script, the entire script is a run handler.) Even without the “to run” or alternatively “on run” construction, compiled scripts will normally run the “root” level of the script (exclusive of handlers).

If the script has an open handler:

on open(theItems)
        -- operate on theItems
end open

then the applet becomes a droplet: if the user drags and drops file system items onto the compiled script, it will be launched and the open handler will execute the script between on open and end open on a list of aliases to the dropped items.

(Note: “on” is a synonym for “to” when starting a handler; for example, “on run” instead of “to run”.)

A third type of script is very useful: an idle handler. An idle handler inserted in a script that is compiled as a “stay open” application will execute the script items in the idle handler cyclically on a regular timed basis. Idle handlers have the following structure

on run
	-- do stuff --
end run

on idle
	tell application "System Events"
		if not (exists process "ScreenSaverEngine") then
			-- do your thing
		end if
	end tell
	return 5 -- do this every 5 seconds
end idle

on quit
	-- do stuff - assumes something in the idle handler instructs the application to quit and there is some cleanup to do.
	continue quit -- essential
end quit