Writing Scripts

Clockworks interacts with your AppleScripts in two ways. First it supports a scripting dictionary that allows you to control Clockwork from your script. Here’s a simple script that illustrates those capabilities:

tell application "Clockwork"
    -- get selected timer
    set t to selected timer

    -- many properties can only be changed when a timer is stopped
    stop t

    -- access each timer property
    log days of t as string
    log alarm date of t as string
    log start date of t as string
    log seconds of t as string
    log minutes of t as string
    log running of t as string
    log display time of t as string
    log step interval of t as string
    log subseconds of t as string
    log hours of t as string
    log name of t as string
    log mode of t as string

    -- clear time, set to five minutes, set alarm mode and start.       
    set display time of t to 0
    set minutes of t to 5
    set mode of t to alarm

    -- finish up by starting t
    start t
end tell

Second Clockwork can call your scripts when certain events happen. A simple example of this is when you select a script in the script menu, but a more interesting example is when an alarm timer reaches zero. When the alarm reaches zero the handler “performAlarmAction” will be called in each applescript that is checked in the alarm actions table for that timer. For example:

-- This script implements the performAlarmAction event. To use:
--  1. Move this script into Clockworks Scripts folder (Script Menu > Open Scripts Folder)
--  2. Restart Clockwork
--  3. Create a new alarm and open the "Alarm Actions" drawer. Check the box next to this script in the alarm actions table.
--  4. Start the alarm.
--  5. When the alarm finished this scripts performAlarmAction handler should be called.
using terms from application "Clockwork"

    on alarmStarted(anAlarm)
        log "started " & name of anAlarm
    end alarmStarted

    on alarmStopped(anAlarm)
        log "stopped " & name of anAlarm
    end alarmStopped

    on alarmFinished(anAlarm, anAlarmDate)
        log "finished " & name of anAlarm & " on " & anAlarmDate
    end alarmFinished

end using terms from

This can be used to create a “Yoga timer”. A Yoga timer needs a custom repeat interval because you are supposed to hold positions for (in this case) 20 seconds and then 40 seconds. You can do this with Clockwork with this script:

-- Yoga alarm. 
property repeatInterval : 20

using terms from application "Clockwork"

    on alarmStarted(anAlarm)
        log "started " & name of anAlarm
    end alarmStarted

    on alarmStopped(anAlarm)
        log "stopped " & name of anAlarm
    end alarmStopped

    on alarmFinished(anAlarm, anAlarmDate)
        stop anAlarm
        if repeatInterval = 20 then
            set repeatInterval to 40
        else
            set repeatInterval to 20
        end if

        set seconds of anAlarm to repeatInterval
        start anAlarm
    end alarmFinished

end using terms from