That was ignorant of me, actually I was thinking that someone may have round figures. (envy).
So your solution is perfect. I was also thinking of that, but not implementing it
@ DJ Bazzie Wazzie: open -f seem to activate TextEdit, not just bring the first window to foreground, but it is a sweet onle liner, pity it can’t be combined with the -b switch.
do shell script "system_profiler SPPowerDataType | open -f -a textedit"
edit : Just for fun an one liner to get only the battery percentage
do shell script "x=($(ioreg -l | egrep -i '(max|current)capacity' | sed s/[^0-9]//g)); echo ${x[1]} / ${x[0]} '* 100' | bc -l | xargs printf %.2f%%"
or to get is an number
run script (do shell script "x=($(ioreg -l | egrep -i '(max|current)capacity' | sed s/[^0-9]//g)); echo ${x[1]} / ${x[0]} '* 100' | bc -l | xargs printf %.2f")
I use run script so the given number can be coerced into an number no matter which locale settings. English notations shouldn’t have to use run script.
Here is one that are a little bit more than a line, but shows you your cycles left before the capacity of the battery is down to 80 percent!
set cycles to (do shell script "system_profiler SPPowerDataType | sed -n '/[Cc]ycle count/ s/\\([ \\t]\\{1,\\}[^0-9]\\{1,\\}\\)\\([0-9]*\\).*$/\\2/p'") as number
display dialog "You have : " & (1000 - cycles) & " cycles left!"
# Number of initial cycles will vary! Check with Apple!
Edit
It is also nice to know about a way to coerce a float correctly.
If somebody asks me why i just didn’t do that like this:
set cycles to (do shell script "system_profiler SPPowerDataType | sed -n '/[Cc]ycle count/ s/[^0-9]*\\([0-9]*\\)/\\1/p'") as number
Then I’d say I wanted to show off the modifiers, where {0,} means nought or more (*), {1,} means one or more (+) and {0,1} means zero or 1, which is the way to specify the modifiers with basic regular expressions.
property PowerIcon : a reference to file ((path to library folder from system domain as text) & "PreferencePanes:EnergySaver.prefPane:Contents:Resources:EnergySaver.icns")
Nice. Rather disturbingly, it works as a property, without recompiling, when the script’s transferred my other computer, whose startup disk has a different name. However, it only works in Script Editor. It errors reassuringly in Script Menu unless recompiled on that machine.
Hi everyone! Sorry that this is a bump, but I stopped working on Battery Monitor in 2011 and I was delighted to see some community members still working to try and find ways to improve it. Today, I came across the old script files, and sure enough, Battery Monitor wasn’t working on 10.8 (I wrote it back in 10.6, so I’m not surprised). As some of you noticed, the string names were capitalized, so I had to do some minor tweaking to get this to work. After a few hours of work, I’m pretty pleased with the changes. Let me know what y’all think. In a way, I’m kind of proud that we got this to work just in AppleScript. I’m also including a link to download the binary file, which is actually needed to run this (due to referencing self-contained images to be shown in dialog boxes). I did the art a long time ago, and I’m planning on making it look sexier. Anyways, enough talk. Here’s version 3.0.0. I just realized I only credited StefanK. Sorry guys, I’ll add you to the creds in next version.
--Battery Monitor 3.0.0
--Written by Panah Neshati, with help from StefanK
--Terms and Conditions: http://www.gnu.org/licenses/gpl.html
--Changes:
----Simplified the interface, removed lots of unneeded icons.
----Now writes data to an actual log file.
----Icons will soon get a makeover.
on roundThis(n, numDecimals)
set x to 10 ^ numDecimals
(((n * x) + 0.5) div 1) / x
end roundThis
try
set battery_data to paragraphs of (do shell script "system_profiler SPPowerDataType | awk '/(Charge Remaining)|(Full Charge Capacity)|(Cycle Count)|(Amperage)|(Voltage)/ {print $NF}'")
set current_charge to item 1 of battery_data
set max_charge to item 2 of battery_data
set charge_cycles to item 3 of battery_data
set amps to item 4 of battery_data
set volts to item 5 of battery_data
on error
display dialog "Unable to retrieve battery information." with title "Battery Monitor" with icon 1
quit
end try
set amps to roundThis(((amps ^ 2) ^ 0.5 / 1000), 2)
set volts to roundThis(volts / 1000, 2)
set pct to (roundThis((current_charge / max_charge) * 100, 1))
--& "%") as string
set icon_disp to (((path to me) & "Contents:Resources:Icons:" & (round roundThis((pct) as number, -1)) & ".icns" as string))
display dialog "Charge: " & pct & "%
Cycles: " & charge_cycles & "
Amperage: " & amps & " amps
Voltage: " & volts & " V" with title "Battery Monitor" buttons {"Log", "OK"} default button 2 with icon alias icon_disp
if button returned of the result is "Log" then
set m to month of (current date)
set d to day of (current date)
set y to year of (current date)
set temp to (m & " " & d & ", " & y) as string
set battery_log to open for access POSIX file "/var/tmp/Battery.log" with write permission
write (((current date) & "
-------------------------
Current Charge: " & current_charge & " mAh
Maximum Charge: " & max_charge & " mAh
Charge Cycles: " & charge_cycles & "
Amperage: " & amps & " amps
Voltage: " & volts & " V
-------------------------
") as string) to battery_log starting at eof
close access battery_log
tell application "Finder" to reveal POSIX file "/var/tmp/Battery.log"
end if
Any ideas for other features to add/tidbits of info the user might find interesting/helpful?
Thanks in advance. You guys gave me the courage to work on this much longer than I otherwise would have. Also, sorry about taking forever to do this. Mostly I forgot about it, but I also had trouble finding my MacScripter login (and my email changed).
Let me know what you think of the logs. Should they provide more info than the dialog box, or the same info?
Panah
Model: MacBook Pro
AppleScript: 2.2.4
Browser: Firefox 23.0
Operating System: Mac OS X (10.8)