Timing my external battery

Thought I would write a quick little script to time how long my external battery supplies juice. I know I’m missing something really simple, any help would be appreciated.


repeat
	tell application "System Events"
		set Power to word 1 of (do shell script "pmset -g |grep '*'")
	end tell
	if Power = "AC" then
		set start_Time to (current date)
	else
		display dialog (current date) - start_Time
	end if
end repeat

I run the script then unplug my power and I keep getting “0” in the display window. I want the end result to display hh:mm:ss, but would be really happy if I could just get it displaying the proper seconds.

Thanks in advance

Hi,

of course you get 0 the first time, but after a few loops the number should change.
This adds the formatting of the string in hh:mm:ss

System Events is not needed to call a shell script


repeat
	set Power to word 1 of (do shell script "pmset -g |grep '*'")
	if Power = "AC" then
		set start_Time to (current date)
	else
		tell ((current date) - start_Time) to set {hr, mn, sc} to {it div hours, it mod hours div minutes, it mod hours mod minutes div 1}
		set HrMnSc to addZero(hr) & ":" & addZero(mn) & ":" & addZero(sc)
		display dialog HrMnSc
	end if
end repeat

on addZero(v)
	return text -2 thru -1 of ("0" & v)
end addZero

What’s wrong with:

time string of (current date)

??? :open_mouth:

Are you trying to get the time between stopping charging and when it runs out of battery?

Thanks StefanK.

I’m still getting a 0 result when I run the script, wait a couple of seconds, then unplug. Now by fluke I had to unplug and take my laptop to work and when I got in I ran the script (using battery power) and it gave me a count 00:54:23. I want the script to start when I plug in an external battery (this will charge my internal battery - essentially “AC” power), then when it runs out of power…I want it to stop.

I’m on an iMac so I have no idea if it starts or stops at the right time, I’m sure you can swap the ifs around to get it right :slight_smile:


global valfile
if button returned of (display dialog "Make new or use existing .txt file?" buttons {"Quit", "New", "Open"} default button 3 cancel button 1 with title "Battery Timer" with icon note) is "Open" then
	set valfile to (choose file of type {"public.plain-text"} with prompt "Please select a .txt file:")
else
	set valfile to (choose file name with prompt "Please choose the name and location of the new file:") & ".txt" as string
	open for access valfile
	set valfile to valfile as alias
	close access valfile
end if

repeat
	tell application "System Events"
		set Power to word 1 of (do shell script "pmset -g |grep '*'")
	end tell
	if Power = "AC" then
		--else
		set start_Time to time string of (current date)
		set valtimer1 to time of (current date)
		exit repeat
	end if
end repeat

repeat
	tell application "System Events"
		set Power to word 1 of (do shell script "pmset -g |grep '*'")
	end tell
	set end_Time to time string of (current date)
	set valtimer2 to time of (current date)
	set valtimer to valtimer2 - valtimer1
	set valtimerstng to subcalctime(valtimer)
	if Power = "AC" then
		subwrite("AC power started at: " & start_Time & "
AC power stopped at: " & end_Time & "
	    Time on: " & valtimerstng)
	else
		exit repeat
	end if
	delay 1
end repeat

on subwrite(valwrite)
	open for access valfile with write permission
	set eof valfile to 0
	write valwrite as string to valfile
	close access valfile
end subwrite

on subcalctime(valtime)
	set valhrs to subaddzero(round valtime / 60 / 60 rounding down)
	set valmin to subaddzero(round (valtime - valhrs * 60 * 60) / 60 rounding down)
	set valsec to subaddzero((valtime - valhrs * 60 * 60) - (valmin * 60))
	return valhrs & ":" & valmin & ":" & valsec as string
end subcalctime

on subaddzero(valnum)
	return text -2 thru -1 of ("0" & valnum)
end subaddzero

That’s awesome. Thanks Richard and StefanK

:slight_smile: Happy that was time well spent. :smiley: