Advertisement
Guest User

Untitled

a guest
Apr 4th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. log "BatteryMonitor: Launching..."
  2.  
  3. set theCounter to 60
  4. set theInterval to 2
  5. set okBtnText to "Go Ahead!"
  6. set Pct to 100
  7. set minBattLvl to 20 -- Min Battery Lvl
  8.  
  9. repeat until theCounter is 0
  10.    
  11.     set Cap to (do shell script "ioreg -w0 -l | grep ExternalChargeCapable")
  12.     tell Cap to set {wallPower} to {last word of paragraph 1}
  13.     if wallPower = "Yes" then
  14.         return 0
  15.     else
  16.         set Cap to (do shell script "ioreg -wO -l | grep Capacity")
  17.         tell Cap to set {Available, Max} to {last word of paragraph 2, last word of paragraph 1}
  18.         set Pct to round (100 * Available / Max)
  19.     end if
  20.    
  21.     if Pct > minBattLvl then
  22.         return 0
  23.     end if
  24.    
  25.     tell me to activate
  26.    
  27.     set usrResponse to display dialog ¬
  28.         "Battery below min safe level of " & minBattLvl & "%.
  29.  
  30. Forcing sleep in: " & theCounter & ¬
  31.         " seconds" as text buttons {"Cancel", okBtnText} ¬
  32.         default button ¬
  33.         okBtnText with icon caution with title ¬
  34.         "WARNING: Battery Monitor" giving up after theInterval
  35.    
  36.     if button returned of usrResponse is "Cancel" then
  37.         return 0
  38.     end if
  39.    
  40.     if button returned of usrResponse is okBtnText then
  41.         exit repeat
  42.     end if
  43.    
  44.     -- delay 5
  45.     set theCounter to theCounter - theInterval
  46. end repeat
  47.  
  48. -- Log entry in syslog
  49. do shell script "logger 'BatteryMonitor: Battery level low below " & minBattLvl & "%, forcing sleep...'"
  50.  
  51. -- Hybernate/Sleep command
  52. do shell script "pmset sleepnow"
  53.  
  54. -- DEBUG display dialog "Here we sleep..."
  55.  
  56. -- Delay exit for allow for sleep/wake
  57. delay 30
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement