How to Run a Script When the Battery is Charged

You can create an event in Power Manager to automatically perform tasks when the computer's battery reaches a specific charge. In this recipe, we will create an event that runs a script when the battery reaches 90% charged.

Power Manager already includes a Schedule Assistant task for running scripts when the battery charge drops, but not one for when the battery charge rises. This recipe shows how to create a battery rising triggered event using Power Manager's built-in Event Editor.

Let's walk through creating an automatic run script on power remaining event.

  1. Launch and unlock Power Manager.app Launch Power Manager on macOS
  2. Add a new empty event; select the menu item: File > New > New Event… New empty event
  3. Open the event in the Event Editor; select the event, then select the menu item: Edit > Edit in Event Editor Open the event in the Editor
  4. Add a Power Remaining trigger; select the pop-up menu item: Add a trigger > Power Remaining > Battery 90% Charged Add a Power Remaining trigger The trigger can be edited to adjust the power source involved and charge behaviour.
  5. Add a Run a Script action; select the pop-up menu item: Add an action > Run Script > Shell Script Add a Run Script action The initial shell script does nothing.
  6. Edit the script action to speak when performed; select Edit from the Action's pop-up menu (top-right … icon), or double-click on the action.
  7. Extend the script with the say command:

     #!/bin/sh
     say "Battery 90% charged."
    

    Edit the embedded shell script

  8. Save the changes to the Run Script action.
  9. Name the event and add the Optional behaviour Can Perform On Demand; select Edit from the Event's pop-up menu (top-right … icon), or double-click on the event name.

    Change the name to Nearly Charged.

    Select the pop-up menu item Optional > Behaviours and select the checkbox Can Perform On Demand.

    Name the event

  10. Apply and Save to confirm the changes and update the event. The newly created automatic power remaining event in Power Manager

The power remaining event is now ready to test.

By adding the Can Perform On Demand behaviour, you can test the event's embedded script works separately from the event's power remaining trigger. Try triggering the event manually using the Power Manager status menu bar, or through the application. You should hear the computer speak. If this works, you know the action and it's script are correct.

Next test the event's power remaining trigger. This can be done by using the computer's battery and then charging it back up. When the battery's charge percentage passes 90%, the computer should speak.

The Power Remaining trigger can be customised:

Power Remaining trigger in Power Manager

The same trigger handles both triggering on the charge rising and falling. It can trigger based on percentage capacity or time remaining. The trigger can also monitor power sources such as Uninterruptable Power Supplies (UPS).

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

Published by Graham Miln on