Wednesday, March 29, 2017

#1 2006-01-26 07:16:16 pm

Bill Cheeseman
Administrator
Registered: 2006-06-29
Posts: 158

Rerunning Applets with AppleScript

Rerunning Applets

When a user runs (or "opens") a script application,for example, by double-clicking on its icon or selecting its icon and choosing the "Open" command from the File menu, the Finder executes the applet's run handler. If the applet has no explicit run handler, it executes any code at the root level of the script as an "implicit" run handler.

Scripters sometimes assume that the run handler will be executed every time an applet is run in this manner. The assumption is correct, but only if the applet was not saved as a "stay-open" application. Non-stay-open applets automatically quit when they complete all of their scripted actions, and running them again causes them to be relaunched.

Quitting and relaunching takes time, however, and it occurs to many scripters that saving an applet as a stay-open application will save this lost time in the case of applets that are intended to be run repeatedly. Alas, double-clicking or otherwise opening an already-running applet does not call its run handler, and nothing happens. What was, prior to Mac OS 8.5, called the Required Suite of Apple events does not actually include a run event but an open application event, and an applet that is already open (running) is not sent an open application event.

In Mac OS 8, a new reopen application event was implemented, and it is still with us in Mac OS X today. It is automatically sent to every stay-open application that is already running whenever a user attempts to open it again. To take advantage of the reopen application event, virtually every stay-open applet that might be run repeatedly by a user should include a reopen handler. Normally, the reopen handler should simply call the run handler, but other actions may be appropriate. For example, a run handler could include initialization routines that are omitted from the reopen handler

Here's how to implement a reopen handler in a stay-open applet. Be sure to save this script as an application or application bundle and check the Stay Open checkbox.

Applescript:


on run
beep 1
--Hear only one beep the first time you run me
end run

on reopen
beep 2
run
--Hear three beeps the second time you run me
end reopen

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.029 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)