How to Perform an Event with AppleScript

Power Manager lets you build events capable of performing complex sequences of actions. Power Manager also lets you schedule these events to occur on specific triggers, such as the time of day or after user inactivity. What if no trigger suits your needs? In this situation, you will want to perform the event yourself.

Let's look at how to perform an event using AppleScript. Power Manager exposes its entire range of capabilities through AppleScript, and being able to perform an event is included.

Event detail showing in the Power Manager System Preference on Mac OS X

I am going to assume you have an event you want to perform.

Take a look at your event using either the Power Manager System Preference, or with Power Manager Professional. Make sure the event has the Can Perform On Demand Behaviour set.

A Power Manager event with On Demand behaviour enabled.

To write the shortest AppleScript, you will need the unique identifier (ID) of your Power Manager event.

Perform an Event by Unique Identifier

The following AppleScript asks Power Manager to perform the event with a specific unique identifier:

tell application "/Library/Application Support/Power Manager/Agents/Power Manager Scripting.app"
    
    tell On Demand to perform event with id "3C6C93FC-2D4C-4514-96ED-5867A0E25B34"
    
end tell
An AppleScript to perform a Power Manager event by unique identifier (ID).

Using the unique identifier means the name of the event can be changed without affecting your AppleScript.

Perform an Event by Name

Having to embed an event's unique identifier into an AppleScript is not always welcome. The odds of making a mistake in the unique identifier are fairly high. Let's look at how to perform an event using only the event's name:

tell application "/Library/Application Support/Power Manager/Agents/Power Manager Scripting.app"
    
    tell On Demand
        set myEventID to trigger ID of events whose name is "Lab Event"
        perform event with id myEventID
    end tell
    
end tell
An AppleScript to perform a Power Manager event by name.

This AppleScript adds an extra couple of lines. The main difference is how the AppleScript finds the event's unique identifier.

The unique identifier is looked up by name and stored in the myEventID variable. This variable is then passed to the perform command in place of the long string of letters and numbers we previously used.

Why Unique Identifiers

Power Manager uses unique identifiers because they are unique within your schedule. Multiple events can share the same name, but never the same unique identifier.

By using using unique identifiers we can avoid the potential problem of matching multiple events when performing the event search.

Where possible prefer unique identifiers in your AppleScripts. If this is not possible, be prepared to deal with events sharing matching names – and names that can be fully internationalised.

This article was posted in AppleScript, PM4, PMPRO4, Power Manager, and Recipe and tagged event, and perform on 2012-02-01.