Shell · Inline
Battery Health
Shows the battery's charge, condition, maximum capacity and cycle count without opening System Information.
- Language
- Shell
- Output
- Inline
- Keyword
battery- Arguments
- None
- Timeout
- 20 s
- Licence
- MIT
- Author
- Docktopus
- File
- battery-health.zsh (1.0 KB)
- Downloads
- 0
In Docktopus: Settings → Scripts → Browse Community Scripts. Read the source before you run any script.
Source
battery-health.zsh · sha256 6448d9e4668a…#!/bin/zsh
# @docktopus.schemaVersion 1
# @docktopus.title Battery Health
# @docktopus.mode inline
# @docktopus.icon battery.75percent
# @docktopus.keyword battery
# @docktopus.description Shows the battery's charge, condition, maximum capacity and cycle count without opening System Information.
# @docktopus.timeout 20
# @docktopus.author Docktopus
info=$(/usr/sbin/system_profiler SPPowerDataType 2>/dev/null)
if [[ "$info" != *"Cycle Count"* ]]; then
echo "This Mac has no battery."
exit 0
fi
field() {
print -r -- "$info" | /usr/bin/awk -F': ' -v key="$1" '$1 ~ "^ *"key"$" { print $2; exit }'
}
charge=$(/usr/bin/pmset -g batt | /usr/bin/grep -Eo '[0-9]+%' | /usr/bin/head -1)
source=$(/usr/bin/pmset -g batt | /usr/bin/head -1 | /usr/bin/sed -E "s/^Now drawing from '(.*)'$/\1/")
echo "Charge: ${charge:-unknown} (${source:-unknown})"
echo "Condition: $(field Condition)"
capacity=$(field 'Maximum Capacity')
[[ -n "$capacity" ]] && echo "Maximum capacity: $capacity"
echo "Cycle count: $(field 'Cycle Count')"