Chapter 3. Scripting the Event Store

Example 3.1. Counting Events

Get the number of events.

tell application "Power Manager"
    tell Event Store
        set numEvents to count events
    end tell
end tell

Example 3.2. Counting Pending Triggers

Get the number of pending triggers.

tell application "Power Manager"
    tell Scheduler
        set numTriggers to count queued triggers
    end tell
end tell

Example 3.3. Looking for Existing Events

Say "Morning exists." if an event named "Morning" exists.

tell application "Power Manager"
    tell Event Store
        if exists (events whose name is "Morning") then
            say "Morning exists."
        end if
    end tell
end tell

Example 3.4. Make An Event

Creates a new sleep event triggered once, in ten minutes.

tell application "Power Manager"
    
    -- Create the event in the workshop 
    tell workshop
        
        -- Create a new event
        set myEvent to make new event with properties {name:"Sleep in ten minutes"}
        
        -- Create a new trigger
        make new trigger once with properties {date:((current date) + 900)} at front of triggers of myEvent
        
        -- Create a new action
        make new action sleep at front of actions of myEvent
        
    end tell
    
    -- Deploy the event
    tell Event Store to store these events myEvent
    
    -- Clean up the workshop
    empty workshop
    
end tell

Example 3.5. Copy an Event

Find an event by name, copy it to the workshop, and deploy with a new identifier.

tell application "Power Manager"
    
    -- Copy the matching event to the workshop
    duplicate the (first event whose name is "Morning") of the Event Store to the end of the events of the workshop
    set myEvent to the last event in the workshop
        
    -- Remove the id to avoid overwriting original
    delete id of myEvent
    
    -- Without the id, we need to get a new reference to the event
    set myEvent to the last event in the workshop
    
    -- Deploy the new event
    tell Event Store to store these events myEvent
    
    -- Clean up the workshop
    empty workshop
    
end tell

Example 3.6. Rename an Event

Finds an event by unique identifier, modifies the event's name, and resubmits the updated copy.

tell application "Power Manager"
    
    -- Copy the matching event to the workshop
    duplicate the (first event whose id is "29531439-1319-4881-B980-F55424CE6F2B") of the Event Store to the end of the events of the workshop
    set myEvent to the last event in the workshop
    
    -- Modify the workshop copy
    set name of myEvent to "Another Name"
    
    -- Deploy the modified event
    tell Event Store to store these events myEvent
    
    -- Clean up the workshop
    empty workshop
    
end tell

Example 3.7. Disable an Event

Finds an event by name, disables it, and resubmits the updated copy.

tell application "Power Manager"
    
    duplicate the (first event whose name is "Morning") of the Event Store to the end of the events of the workshop
    set myEvent to the last event in the workshop
    
    -- Modify the workshop copy
    set myEvent's enabled to false
    
    -- Deploy the modified event
    tell Event Store to store these events myEvent
    
    -- Clean up the workshop
    empty workshop
    
end tell

Example 3.8. Toggling an Event On and Off

This AppleScript handler finds an event by name, toggles the event's enabled state, and resubmits the updated copy.

ToggleEvent("My Event")

-- Toggle the enabled state of a single Power Manager event
on ToggleEvent(anEventName)
    
    -- Catch errors
    try
        
        -- Talk to Power Manager's scripting application
        tell application "Power Manager"
            
            duplicate the (first event whose name is anEventName) of the Event Store to the end of the events of the workshop
            set myEvent to the last event in the workshop
            
            -- Modify the workshop copy; toggle the enabled state
            set myEvent's enabled to (not myEvent's enabled)
            
            -- Deploy the modified event
            tell Event Store to store these events myEvent
            
            -- Clean up the workshop
            empty workshop
            
        end tell
        
    on error (theError)
        
        -- Something went wrong
        display dialog "Error toggling '" & anEventName & "': " & theError
        
    end try
    
end ToggleEvent

Example 3.9. Remove an Event by Name

Removes an event by name.

tell application "Power Manager"

    tell Event Store
        remove event with id (id of first event whose name is "Morning")
    end tell
    
end tell

Example 3.10. Remove all Events

Remove all events from the schedule.

Fetches all the unique ids into a list and passes each item of the list to the remove command.

tell application "Power Manager"

    tell Event Store
        
        set everyID to id of every event
        
        repeat with anID in everyID
            remove event with id anID
        end repeat
        
    end tell
    
end tell

Example 3.11. Get an Event's History

Get the history of an event.

tell application "Power Manager"

    tell Event Store
        
        set myEventID to id of first event
        set myHistory to history for event with id myEventID
        
    end tell
    
end tell

Example 3.12. Get an Event's Last Started Date

Get the date an event was last started. If the event has not been performed, the date will be missing.

tell application "Power Manager"

    tell Event Store
        
        set myHistory to history for event with id (id of first event)
        set myLastStarted to last started of myHistory
        
    end tell
    
end tell

Example 3.13. Has an Event Been Performed

Find out if an event has been performed using the event's history.

tell application "Power Manager"

    tell Event Store
        
        set myHistory to history for event with id (id of first event)
        set eventHasPerformed to exists (last started of myHistory)
        
    end tell
    
end tell