Using an External Script as an Event Condition

You can augment Power Manager's built-in energy saving functionality with shell scripts and other external tools. This article shows you how to create an event that uses an external script as a condition.

Power Manager has been designed to allow for easy integration with other tools and workflows. We rely on this ability to easily extend Power Manager to help us trial and test new triggers, conditions, and actions before enshrining them into the formal event specification.

The ability to integrate is provided by events that can be triggered on demand, and through an external condition and an external action.

Being able to be triggered on demand allows Power Manager events to be launched by other scheduling tools, such as cron and launchd.

External conditions and actions allow third party scripts and tools to augment the event sequence.

We are going to look at the external condition.

External conditions allow events to call out to external scripts and processes. The external script or process is asked if their condition is met. What that condition is, Power Manager does not know; that is for the external script or process to determine and test.

External conditions are powerful. With an external condition you can test almost any value, setting, or state available to your Mac. You might test if a particular file exists, or if a certain application is busy. You might test if a web page can be reached or if a particular e-mail remains unread. The possibilities are unlimited.

The ability to create an external condition is not exposed in the standard Power Manager interface. You need Power Manager Professional, or to use one of the alternative interfaces - such as AppleScript or the included command line tool pmctl.

To demonstrate how to create a Power Manager event that uses an external condition, we are going to use the AppleScript interface. This will work with every edition of Power Manager.

Conditionally Sleep After 15 Minutes

The event will put the Mac to sleep after 15 minutes of user inactivity, but only if a particular file exists. If the file does not exist, the event will not put the Mac to sleep.

  1. Launch AppleScript Editor: Applications > Utilities > AppleScript Editor.
  2. Copy and paste the following AppleScript into a new document:
    -- The script to run when testing the condition
    set myScriptContents to "#!/usr/bin/perl
    exit (-e '/Users/Shared/no-sleep.txt');
    tell application "/Library/Application Support/Power Manager/Agents/Power Manager"
        tell workshop
            set myEvent to make new event with properties {trigger ID:"externalSleep", name:"Conditionally Sleep"}
            -- Create an inactivity trigger for 15 minutes
            make new trigger after inactivity with properties {seconds of inactivity:(60 * 15)} at end of triggers of myEvent
            -- Create an external inline script to test for a file's existence
            set myExternal to make new external inline with properties {inline:myScriptContents}
            -- Create a condition using the external
            set criteria of myEvent to make new criteria
            make new condition execute external with properties {external:myExternal} at end of conditions of criteria of myEvent
            -- Create a warn action before sleeping
            make new action warn at end of actions of myEvent
            -- Create a sleep action after the warning
            make new action sleep at end of actions of myEvent
            -- Mark the event as having on-demand behaviour
            set behaviours of myEvent to [can perform on demand]
        end tell
        -- Deploy the event
        tell Event Store to store these events myEvent
        -- Clean up
        tell the workshop to empty
    end tell
    AppleScript to create a Power Manager event with an external condition.
  3. Save the script: File > Save.
  4. Run the script: Script > Run.

When run, the above script, creates a Power Manager event called 'Conditionally Sleep'. The event contains a single trigger, a condition, and two actions.

Below is how the new event looks in Power Manager Professional. Using Professional's event editor, you can see the different sections of the event and how the script has put those sections together.

The event editor in Power Manager Professional.

Testing the Event

The event will be triggered after 15 minutes of inactivity and can also be triggered on demand. You can use the on demand trigger to test the event works. Try triggering the event using the status menu bar, or through Power Manager Remote on your iOS device.

Your Mac will immediately sleep unless a file named no-sleep.txt exists in a particular folder. The folder being checked is: Users > Shared.

Try triggering your event after placing a file named 'no-sleep.txt' in /Users/Shared/. Note your Mac does not sleep. This is because the event has called out to the external script to test if the file exists; the file does exist, so the event's actions are not performed.

Self-Contained Event

This event is self-contained. The external script is included inside the event itself. This is not required and Power Manager can be told to use scripts stored elsewhere on your Mac. However, inline scripts benefit from being less fragile. The script is always available and its association with the event is clear.

As you have seen, Power Manager can be extended with external scripts. The script can be as simple or as complex as you desire. Should you ever find Power Manager's built-in functionality not quite enough for your needs, consider using an external condition or action.

This article was posted in , , , , , and and tagged , , , , and .

Published by Graham Miln on