You can script your Mac to perform actions, such as running a command or launching an application, when your uninterruptible power supply (UPS) switches over to the emergency battery.
If your Mac's UPS hardware has switched to battery power, then it is likely the mains power has failed and you need to react as soon possible. Being able to automate your response will save you time and stress when the power fails.
We have previously walked through how to perform a command when a laptop switches from mains power to battery power. Today we are going to modify that recipe to deal with UPS hardware.
As a response to the power failing, let's make the Mac speak when the UPS switches over to battery power. With Power Manager you can schedule an application to launch, documents to open, or any other command or script you like.
Create a UPS Triggered Command Line Event
- Launch System Preferences and select Power Manager.
- Click Add… to create a new event.
- Choose the template Run a script on battery.
- Click Continue to the Script step.
- Copy and paste your shell script into the Script step.
#!/bin/sh say "Switching to UPS battery power."
Continue through the remaining steps until your event is created.
At this point, the event will be triggered by any connected power source switching to battery power. For our purposes, we are going to modify the event to trigger only when a UPS changes to battery. To do this we use Power Manager's event editor.
- Open your event using the event editor; hold down the Option key on the keyboard and double-click on the event.
- Edit the event's trigger; click on the cog in the top-right of the trigger and select Edit.
- Add the optional Filter setting to the trigger; click on Optional pop-up menu in the bottom-left of the trigger and select Filter.
- Select USB Connected Power Supply from the Filter pop-up menu. If your UPS is connected via a serial cable or over the network, choose the appropriate alternative.
- Click Apply at the bottom-right of the trigger.
Note the new Filter by power source text that appears under the trigger's description.
- Click Save to confirm your changes to the event.
After following the above steps, your UPS event is scheduled and ready.
To test your new event, try disconnecting your mains power from your UPS. A moment after disconnecting, your Mac will speak and warn that you are now running on battery power.