Guest User

Untitled

a guest
Apr 8th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.40 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # How long to display countdown dialog
  4. cntDown=60
  5.  
  6. # Dialog timeout
  7. dlgTimeout=2
  8.  
  9. # Minimum battery level to reach before forcing hibernation
  10. minBattLvl=20
  11.  
  12. # Replacement text for OK dialog box button
  13. btntext="Proceed"
  14.  
  15. while [[ $cntDown -gt 0 ]] ; do
  16.    ioreg -w0 -l | grep ExternalChargeCapable | grep -q Yes
  17.    if [[ $? -ne 0 ]] ; then
  18.       max="`ioreg -w0 -l | grep MaxCapacity | cut -d= -f 2`"
  19.       avail="`ioreg -w0 -l | grep CurrentCapacity | cut -d= -f 2`"
  20.       pct=$(($avail*100/$max))
  21.    else
  22.       exit 0   # We are charging, nothing to do....
  23.    fi
  24.  
  25.    userResp="`/usr/bin/osascript 2>&1 <<-EOF
  26.    tell application "System Events"
  27.        activate
  28.        display dialog \
  29.         "Battery below min safe level of ${minBattLvl}%" \
  30.         & "\\n\\nForcing hibernation in: ${cntDown} seconds." \
  31.         as text buttons {"Cancel","$btntext"} \
  32.         default button "$btntext" \
  33.         with icon caution \
  34.         with title "WARNING: Battery Monitor" \
  35.         giving up after $dlgTimeout
  36.    end tell
  37. EOF`"
  38.  
  39.    echo $userResp | grep -q "User canceled"
  40.    abort=$?
  41.    [[ $abort -eq 0 ]] && exit 0
  42.  
  43.    echo $userResp | grep -q $btntext
  44.    proceed=$?
  45.    [[ $proceed -eq 0 ]] && let cntDown=0
  46.  
  47.    let cntDown-=2
  48. done
  49.  
  50. # Log it in syslog
  51. logger "BatteryMonitor: Battery level below ${minBattLvl}%. Forcing hibernation..."
  52.  
  53. # Activate sleep/hibernation`
  54. pmset sleepnow
Advertisement
Add Comment
Please, Sign In to add comment