Skip to content

AppleScript to Act Like a UPS Monitor

February 5, 2012

If you have ever bought a cheap (or Chinese) UPS for your Mac’s battery backup, you may find that the UPS either has no USB connection, no software or your Mac just does not recognize the device. The purpose of having a UPS is to have backup power in case of power failure but if you don’t have software to tell the Mac to shut down when your running on battery power than your Mac may still shut down unexpectedly when the battery runs out. 

To Fix this, I have created an AppleScript that will monitor your your connection to another device on your network and when needed shut down. Let me explain; Not everything needs to have battery backup. I dont have my ADSL modem on battery backup so if the power goes out, I lose my internet and my connection to the modem. My AppleScript pings the modem every minute and if there is no response for 3 minutes from the modem, the computer shuts down preventing bad things from happening. 

Copy the code below, paste into AppleScript and save as an Application. Then set this to startup when you log into your computer. 

The Only change that needs changed is the password for shutting down the computer and the IP address of a device on your network that also loses power during an outage. 

 


set t to 0
repeat until t = 3
delay 60
if (do shell script "ping -c1 172.16.10.66 2>&1; exit 0") contains "1 packets received" then
set t to 0
else
set t to t + 1
end if
end repeat
do shell script "date > $HOME/Desktop/LastPowerOutage.txt"
do shell script "shutdown -h now" password "******" with administrator privileges

 

From → Tutorial

Leave a Comment

Leave a comment