Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # How long to display countdown dialog
- cntDown=60
- # Dialog timeout
- dlgTimeout=2
- # Minimum battery level to reach before forcing hibernation
- minBattLvl=20
- # Replacement text for OK dialog box button
- btntext="Proceed"
- while [[ $cntDown -gt 0 ]] ; do
- ioreg -w0 -l | grep ExternalChargeCapable | grep -q Yes
- if [[ $? -ne 0 ]] ; then
- max="`ioreg -w0 -l | grep MaxCapacity | cut -d= -f 2`"
- #echo "Max capacity: $max"
- avail="`ioreg -w0 -l | grep CurrentCapacity | cut -d= -f 2`"
- #echo "Available capacity: $avail"
- pct=$(($avail*100/$max))
- #echo "Percent available: $pct %"
- # Do nothing if our battery lvl is still high
- [ $pct -gt $minBattLvl ] && exit 0
- else
- exit 0 # We are charging, nothing to do....
- fi
- userResp="`/usr/bin/osascript 2>&1 <<-EOF
- tell application "System Events"
- activate
- display dialog \
- "Battery below min safe level of ${minBattLvl}%" \
- & "\\n\\nForcing hibernation in: ${cntDown} seconds." \
- as text buttons {"Cancel","$btntext"} \
- default button "$btntext" \
- with icon caution \
- with title "WARNING: Battery Monitor" \
- giving up after $dlgTimeout
- end tell
- EOF`"
- # echo "Response was: '${userResp}'"
- echo $userResp | grep -q "User canceled"
- abort=$?
- #echo "Abort test exit val: $abort"
- [[ $abort -eq 0 ]] && exit 0
- echo $userResp | grep -q $btntext
- proceed=$?
- # echo "$btntext test exit val: $proceed"
- [[ $proceed -eq 0 ]] && let cntDown=0
- let cntDown-=2
- done
- # Log it in syslog
- logger "BatteryMonitor: Battery level below ${minBattLvl}%. Forcing hibernation..."
- # Activate sleep/hibernation`
- pmset sleepnow
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement