SHARE
TWEET

battery health meter

applehelpwriter Aug 25th, 2014 515 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2. batteryHealthMeter
  3. (C) Applehelpwriter.com / Phil Stokes 2014
  4. See: http://wp.me/p1ItrK-ZT
  5. for details
  6. *)
  7.  
  8. on returnNumbersInString(inputString)
  9.         set s to quoted form of inputString
  10.         do shell script "sed s/[a-zA-Z\\']//g <<< " & s
  11.         set dx to the result
  12.         set numlist to {}
  13.         repeat with i from 1 to count of words in dx
  14.                 set this_item to word i of dx
  15.                 try
  16.                         set this_item to this_item as number
  17.                         set the end of numlist to this_item
  18.                 end try
  19.         end repeat
  20.         return numlist
  21. end returnNumbersInString
  22.  
  23. on powerstats()
  24.         set iconAC to "🔌   "
  25.         set iconBatt to "🔋   "
  26.         set iconCharge to "⚡️   "
  27.         set iconTime to "🕒   "
  28.         set iconHealth to "🍀   "
  29.         set iconCycles to "🌀   "
  30.         set iconWarning to "⚠️   "
  31.        
  32.         do shell script "pmset -g everything | grep Cycles"
  33.         set x to the result
  34.        
  35.         set charging to word 2 of x
  36.         if charging contains "Not" then
  37.                 set charger to " (not charging!)   " & iconWarning
  38.         else
  39.                 set charger to " (charging)"
  40.         end if
  41.        
  42.         if word 1 of x is "AC" then
  43.                 set _t to "Time to Full: "
  44.                 set pwr to "AC"
  45.                 set pwrSource to iconAC
  46.         else
  47.                 set _t to "Time Remaining: "
  48.                 set pwr to "Batt"
  49.                 set pwrSource to iconBatt
  50.                 set charger to ""
  51.         end if
  52.        
  53.         set nums to returnNumbersInString(x)
  54.         set percentage to item 1 of nums & "%"
  55.        
  56.         if item 5 of nums = 0 then
  57.                 set hrmins to " mins"
  58.         else if item 5 of nums = 1 then
  59.                 set hrmins to " hr"
  60.         else
  61.                 set hrmins to " hrs"
  62.         end if
  63.         if item 5 of nums is greater than 4 then
  64.                 if pwr is "AC" then
  65.                         set t to "Calculating...try refresh in 2 mins"
  66.                 end if
  67.         else
  68.                 if item 6 of nums is less than 10 then
  69.                         set theMins to item 6 of nums as string
  70.                         set theMins to "0" & theMins as string
  71.                 else
  72.                         set theMins to item 6 of nums as integer
  73.                 end if
  74.                 set t to item 5 of nums & ":" & theMins & hrmins
  75.         end if
  76.        
  77.         set FCC to item 3 of nums as integer
  78.         set designCap to item 4 of nums as integer
  79.         set battHealth to (FCC / (designCap / 100))
  80.         set battHealth to round (battHealth)
  81.         set battHealth to battHealth & "%"
  82.        
  83.         set cycles to item -3 of nums
  84.        
  85.         display dialog pwrSource & "Power Source: " & pwr & charger & return & iconCharge & "Current Charge: " & percentage & return & iconTime & _t & t & return & return & iconHealth & "Battery Health: " & battHealth & return & iconCycles & "Cycles: " & cycles with title "Battery Power Stats" buttons {"Refresh", "OK"} default button "OK" with icon 1
  86.         if button returned of the result is "Refresh" then
  87.                 powerstats()
  88.         end if
  89.        
  90. end powerstats
  91.  
  92. powerstats()
RAW Paste Data
Challenge yourself this year...
Learn something new in 2017
Top