Chapter 3. AppleScript

Table of Contents

Scripting Sleep Monitor

Sleep Monitor includes support for Apple's automation language AppleScript. With AppleScript you can integrate Sleep Monitor in your workflows and automate potentially repetitive tasks.

If you are new to AppleScript, the following two links will give you a good overview of the language and provide resources to get started. Take some time to gain a broader understanding of what it possible, then come back and enjoy the extensive scripting support we provide.

Scripting Sleep Monitor

Example 3.1. Event Properties

Get the properties of individual events logged by Sleep Monitor.

tell application "Sleep Monitor"
    -- get everything about the last recorded event
    set theProperties to properties of the last event
    -- which monitor recorded the middle event
    set theMonitor to monitor of the middle event
    -- count the number of restart events
    set howManyRestarts to number of (events where summary contains "restart")
end tell

Example 3.2. Zoom Out

Adjust Sleep Monitor's visible range to show the last six days.

set days to (24 * 60 * 60)
set newStart to (current date) - (5.9 * days)

-- zoom in to today
tell application "Sleep Monitor"
    set startDay of visibleSlice to current date
    set endDay of visibleSlice to current date
end tell

repeat 30 times
    tell application "Sleep Monitor"
        set startDay of visibleSlice to (startDay of visibleSlice) - (1 * days)
    end tell
end repeat

Example 3.3. Export the Graph

Save the current graph to the Desktop folder as a PNG (Portable Network Graphics) file.

set myFilename to "sleepmonitor-chart.png"

tell application "Sleep Monitor"
    set myPath to ((path to desktop) & myFilename) as string
    export graph visibleSlice to myPath border no title no
end tell