How to Change On-Demand Behaviour With AppleScript

On-demand events are useful. On-demand events appear in Power Manager's status menu bar and can be triggered with a single mouse click from within any application.

On-demand events are useful. On-demand events appear in Power Manager’s status menu bar and can be triggered with a single mouse click from within any application.

The Schedule Assistant creates certain events as on-demand. This is automatic and handled entirely by the Assistant. However, you too can control if an event supports on on-demand behaviour.

Power Manager is fully AppleScriptable. A guide containing sample AppleScripts is included as part of Power Manager.

AppleScript Editor - Power Manager is fully AppleScriptable

Let’s use an AppleScript to modify an existing event named Lunch:

-- Name of the event to edit
set myEventName to "Lunch"

tell application "Power Manager Scripting"

    -- Copy the desired event to the workshop
    duplicate the (first event whose name is myEventName) of the Event Store to the end of the events of the workshop
    set myEvent to the last event in the workshop

    -- Set the event's behaviours
    if behaviours of myEvent contains can perform on demand then
        -- Remove all the behaviours
        delete behaviours of myEvent
    else
        -- Add the on-demand behaviour
        set behaviours of myEvent to [can perform on demand]
    end if

    -- Deploy the modified event
    tell Event Store to store these events myEvent

    -- Clean up the workshop
    empty workshop

end tell

This AppleScript toggles the on-demand behaviour of the event Lunch.

The application Power Manager Scripting can be found in /Library/Application Support/Power Manager/Agents.

Power Manager’s Workshop

Power Manager is not like most AppleScriptable applications. Power Manager is effectively a server, whose settings can not be altered directly by AppleScript.

To support AppleScript, we used the concept of a workshop. The workshop is a local store for AppleScripts to copy events into, and out of.

To modify an event, your AppleScript needs to copy the event from Power Manager into the workshop.

The copy of the event in the workshop is entirely separate from the live copy running within Power Manager. The workshop copy can be altered and changed as needed. Once all the editing is done, you then copy your modified event back into Power Manager.

We used the concept of a workshop because it provides your AppleScripts with the freedom to alter events without the immediate constraint of correctness. Power Manager requires all events to be correct and well formed. There is a good chance that as your AppleScript makes alterations to an event, the event will fall foul of this requirement.

By using a workshop, your copy of the event can be altered freely. The event can be left in an invalid state for as long as needed. Only when the time comes to copy the event back into Power Manager will the event be carefully checked.

If the event is not well formed when you copy it back to Power Manager, a detailed error will highlight what is wrong.

Using a workshop has other benefits. The workshop allows changes to occur locally and at speed. Also, you can run multiple AppleScripts all simultaneously interacting with Power Manager. Power Manager deals with it all seamlessly.