AppleScripting - Part II
Help with AppleScript
the macosxautomation site contains some excellent tutorial information from the very basic to the very advanced and all stops in between.
Idling by: It’s just one state after another
AppleScript has built-in a capability to automatically perform a task based on the IDLE event. It is possible to define a state machine using AppleScript native programming elements.
State machines are the foundation of automated process control systems and basically consist of a set of a series of commands with each series bundled into “tasks”. These tasks are run as required depending on events that take place and are detected by the dispatcher (in the case of AppleScript, the on idle routine). It’s an infinite loop waiting for a recognised stimulus. A task may be terminated by a number of means: time up, all code run, interrupt. How each is handled and whether it is allowed, is up to the designer of the system. For example, space missions have high priority time critical features (like checking the burn time of thrusters!) and so these might override other lower priority tasks such as checking if the on board magnetometer has the correct power.
Using AppleScript, one very interesting feature is how the “time out” for a task is determined, and how the state machine is terminated. You can manually terminate the program or you can give it a time to live. Each task has a default 30 s time slot unless this is overridden by some behaviour such as returning a value from the task.
To give you some idea of how this all works here is a simple model for a state machine. It models the behaviour of a device that is meant to track a light level. A simple case would be a solar cooker device designed to track the sun as it moves across the sky. Notice that some of the time the crudely modelled sense task (task_02 ) actually causes tracking backwards. This is to give it a kind of search behaviour if it looses “sight” of the sun. We could connect an actual sensor and an actual bidirectional motor driver with motor to make this demonstration more interesting. For those who know about micro-controllers and logic circuits, yes, this could be easily implemented with a few components. And for the “other languages” aficionados, yes, it could be implemented more efficiently but this is about AppleScript.
This code must be compiled and run as an application. The behaviour of the idle and run routines do not work when the code is *run* within the Script Editor IDE.
-- Simple State Machine. First worked On: 2007-05-13, last worked on 2008-07-12 -- Ian W. Parker 2007 -- this code must first be compiled to a run-only application (.app) to work properly -- -------------------------------------------------------------- -- the entry state (0) on run random number set end_time to (current date) + 120 * seconds -- just after activating -- <code> end run -- the idle or dispatcher state (1) on idle -- every 30 s enter, process and exit if no other activity -- <code> if (current date) > end_time then quit if task_02() < 5 then -- track left task_01() else -- track right task_03() end if end idle -- the exit state (2) on quit -- just before quitting -- "tidy up" code display dialog "Finishing tasking" giving up after 5 continue quit end quit -- --------------------------------------------------------------- -- Task Set -- --------------------------------------------------------------- on task_01() -- <task code> display dialog "Driving Motor Left" giving up after 5 end task_01 on task_02() -- <task code> display dialog "Measuring Light levels" giving up after 5 return (random number 10) as integer end task_02 on task_03() -- <task code> display dialog "Driving Motor Right" giving up after 5 end task_03