Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Big Sur/Monterey. Supports Apple Silicon. #12

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
# Battery

> For Apple M1 chip users: https://github.com/BenziAhamed/alfred-battery/issues/11#issue-812562822

![Logo](info.png)

An Alfred workflow to display battery information of your Apple laptop.

Is activated by the keyword "battery". The new gradient icon set is the default, but if
you like the old set better, just use the optional keyword "reg", like thus: "battery reg"
and it will revert to the old set.

Gradient Icons:
Thanks to their respective authors on FlatIcon.

Icon Attribution List:

Temperature icons created by Muhammad Ali
Serial icons created by Creatype
Battery icons created by Freepik
Ui icons created by pictranoosa
Health icons created by CLARY K
Ui icons created by Us and Up
Clock icons created by redempticon
Mouse icons created by Ilham Fitrotul Hayat
Panel icons created by Pixel perfect
Computer peripheral icons created by lakonicon
Macbook icons created by bqlqn
Scan icons created by Pause08
Hospital icons created by bearicons
145 changes: 103 additions & 42 deletions src/battery.sh
Original file line number Diff line number Diff line change
@@ -1,78 +1,124 @@
#!/usr/bin/env bash
#!/bin/bash

INFO=$(ioreg -l -n AppleSmartBattery -r)

if [ "$1" = "reg" ]; then
ICON_SET="icons"
else
ICON_SET="gradient-icons"
fi

# Charge and time remaining
CURRENT_CAPACITY=$(echo "$INFO" | grep CurrentCapacity | awk '{printf $3; exit}')
MAX_CAPACITY=$(echo "$INFO" | grep MaxCapacity | awk '{printf $3; exit}')
CHARGE=$((CURRENT_CAPACITY * 100 / MAX_CAPACITY))
CELLS=$(python -c "f='●'*($CHARGE/10) + '○'*(10-$CHARGE/10); print f")
RAW_CURRENT_CAPACITY=$(echo "$INFO" | grep -e \"AppleRawCurrentCapacity\" | awk '{printf $3; exit}')
RAW_MAX_CAPACITY=$(echo "$INFO" | grep -e \"AppleRawMaxCapacity\" | awk '{printf $3; exit}')
DESIGN_CAPACITY=$(echo "$INFO" | grep -e \"DesignCapacity\" | awk -F',' '{printf ("%s", $49)}' | awk -F'=' '{printf ("%i", $2)}')
CHARGE=$((RAW_CURRENT_CAPACITY * 100 / RAW_MAX_CAPACITY))
CELLS=$(/usr/bin/python3 -c "f='●'*(int($CHARGE/9)) + '○'*(int(10-$CHARGE/9)); print(f)")
STATUS_INFO=Draining...

CHARGING=$(echo "$INFO" | grep -i ischarging | awk '{printf("%s", $3)}')
TIME_TO_EMPTY=$(echo "$INFO" | grep -i AvgTimeToEmpty | awk '{printf("%s", $3)}')
CHARGING=$(echo "$INFO" | grep -e \"IsCharging\" | awk '{printf("%s", $3)}')
TIME_TO_EMPTY=$(echo "$INFO" | grep -e \"AvgTimeToEmpty | awk '{printf("%s", $3)}')
TIME_LEFT=Calculating…

if [ "$TIME_TO_EMPTY" -lt 15000 ]; then
TIME_LEFT=$(echo "$INFO" | grep -i AvgTimeToEmpty | awk '{printf("%i:%.2i", $3/60, $3%60)}')
TIME_LEFT=$(echo "$INFO" | grep -e \"AvgTimeToEmpty\" | awk '{printf("%i:%.2i", $3/60, $3%60)}')
fi

if [ "$CHARGING" == Yes ]; then
TIME_FULL=$(echo "$INFO" | grep -i AvgTimeToFull | tr '\n' ' | ' | awk '{printf("%i:%.2i", $3/60, $3%60)}')
TIME_INFO=$(echo "$TIME_FULL" until full)
TIME_FULL=$(echo "$INFO" | grep -e \"AvgTimeToFull\" | tr '\n' ' | ' | awk '{printf("%i:%.2i", $3/60, $3%60)}')
TIME_INFO="$TIME_FULL" until full
STATUS_INFO=Charging...
BATT_ICON=charging.png
BATT_ICON="$ICON_SET/charging.png"
else
FULLY_CHARGED=$(echo "$INFO" | grep -i FullyCharged | awk '{printf("%s", $3)}')
EXTERNAL=$(echo "$INFO" | grep -i ExternalConnected | awk '{printf("%s", $3)}')
FULLY_CHARGED=$(echo "$INFO" | grep -e \"FullyCharged\" | awk '{printf("%s", $3)}')
EXTERNAL=$(echo "$INFO" | grep -e \"ExternalConnected\" | awk '{printf("%s", $3)}')
if [ "$FULLY_CHARGED" == Yes ]; then
if [ "$EXTERNAL" == Yes ]; then
TIME_INFO="On AC power"
STATUS_INFO="Fully Charged"
BATT_ICON=power.png
BATT_ICON="$ICON_SET/power.png"
CHARGE="100"
CELLS="●●●●●●●●●●"
else
TIME_INFO=$TIME_LEFT
BATT_ICON=full.png
BATT_ICON="$ICON_SET/full.png"
fi
else
TIME_INFO=$TIME_LEFT
BATT_ICON=critical.png
BATT_ICON="$ICON_SET/critical.png"
if [ "$CHARGE" -gt 80 ]; then
BATT_ICON=full.png
BATT_ICON="$ICON_SET/full.png"
elif [ "$CHARGE" -gt 50 ]; then
BATT_ICON=medium.png
BATT_ICON="$ICON_SET/medium.png"
elif [ "$CHARGE" -gt 10 ]; then
BATT_ICON=low.png
BATT_ICON="$ICON_SET/low.png"
fi
fi
fi

# Temperature
TEMPERATURE=$(echo "$INFO" | grep Temperature | awk '{printf ("%.1f", $3/10-273)}')
TEMPERATURE=$(echo "$INFO" | grep -e \"Temperature\" | awk '{printf ("%.1f", $3/10-273)}')

# Cycle count
# Cycle Count
CYCLE_COUNT=$(echo "$INFO" | grep -e '"CycleCount" =' | awk '{printf ("%i", $3)}')

# Battery health
# ref: https://github.com/BenziAhamed/alfred-battery/issues/10#issuecomment-745541202
DESIGN_CAPACITY=$(echo "$INFO" | grep DesignCapacity | awk '{printf ("%.i", $3)}')
HEALTH=$((MAX_CAPACITY * 100 / DESIGN_CAPACITY))
# DESIGN_CAPACITY=$(echo "$INFO" | grep DesignCapacity | awk '{printf ("%.i", $3)}')
HEALTH=$((RAW_MAX_CAPACITY * 100 / DESIGN_CAPACITY))

if [ "$HEALTH" -gt 100 ]; then
HEALTH=100
fi
#if [ "$HEALTH" -gt 100 ]; then
# HEALTH=100
#fi

# Serial
SERIAL=$(echo "$INFO" | grep BatterySerialNumber | awk '{printf ("%s", $3)}' | tr -d '"')
SERIAL=$(echo "$INFO" | grep -e \"Serial\" | awk -F',' '{printf ("%s", $40)}' | awk -F'=' '{printf ("%s", $2)}' | tr -d '"')
SERIAL="${SERIAL%\"}"

# Battery age
MANUFACTURE_DATE=$(echo "$INFO" | grep ManufactureDate | awk '{printf ("%i", $3)}')
day=$((MANUFACTURE_DATE&31))
month=$(((MANUFACTURE_DATE>>5)&15))
year=$((1980+(MANUFACTURE_DATE>>9)))
AGE=$(python -c "from datetime import date as D; d1=D.today(); d2=D($year, $month, $day); print ( (d1.year - d2.year)*12 + d1.month - d2.month )")
MANUFACTURE_DATE=$(echo "$INFO" | grep -e \"ManufactureDate\" | awk -F',' '{printf ("%s", $56)}' | awk -F'=' '{printf ("%i", $2)}')

let "MANUFACTURE_DATE=MANUFACTURE_DATE-0x0000303030303030"
let "MANUFACTURE_DATE=(MANUFACTURE_DATE & 0x000000FF00FF00FF) * 10 + ((MANUFACTURE_DATE & 0x0000FF00FF00FF00) >> 8) + 1992"

let "year=(MANUFACTURE_DATE >> 0) & 0xFFFF"
let "month=(MANUFACTURE_DATE >> 16) & 0xFFFF"
let "day=(MANUFACTURE_DATE >> 32) & 0xFFFF"

AGE=$("/usr/bin/python3" -c "from datetime import date as D; d1=D.today(); d2=D($year, $month, $day); print ( (d1.year - d2.year)*12 + d1.month - d2.month )")

TRACKPAD_ICON="$ICON_SET/trackpad.png"
# trackpad
TrackpadPercent=$(ioreg -c AppleDeviceManagementHIDEventService | grep -se \"Magic Trackpad\" -A8 | grep -se \"BatteryPercent\" | sed 's/[a-z,A-Z, ,|,\",=]//g' | tail -1 | awk '{print $1}')
if [ ${#TrackpadPercent} = 0 ]
then
TrackpadTitle="Not connected"
else
TrackpadSlug=$(python -c "f='●'*(int($TrackpadPercent/9)) + '○'*(int(10-$TrackpadPercent/9));print(f)")
TrackpadTitle="$TrackpadPercent% $TrackpadSlug"
fi

MOUSE_ICON="$ICON_SET/mouse.png"
# mouse
MousePercent=$(ioreg -c AppleDeviceManagementHIDEventService | grep -se \"Magic Mouse\" -A8 | grep -se \"BatteryPercent\" | sed 's/[a-z,A-Z, ,|,\",=]//g' | tail -1 | awk '{print $1}')
if [ ${#MousePercent} = 0 ]
then
MouseTitle="Not connected"
else
MouseSlug=$(python -c "f='●'*(int($MousePercent/9)) + '○'*(int(10-$MousePercent/9));print(f)")
MouseTitle="$MousePercent% $MouseSlug"
fi

KEYBOARD_ICON="$ICON_SET/keyboard.png"
# keyboard
KeyboardPercent=$(ioreg -c AppleDeviceManagementHIDEventService | grep -se \"Magic Keyboard\" -A8 | grep -se \"BatteryPercent\" | sed 's/[a-z,A-Z, ,|,\",=]//g' | tail -1 | awk '{print $1}')
if [ ${#KeyboardPercent} = 0 ]
then
KeyboardTitle="Not connected"
else
KeyboardSlug=$(python -c "f='●'*(int($KeyboardPercent/9)) + '○'*(int(10-$KeyboardPercent/9));print(f)")
KeyboardTitle="$KeyboardPercent% $KeyboardSlug"
fi

# Alfred feedback
cat << EOB
Expand All @@ -81,37 +127,52 @@ cat << EOB
<item>
<title>$CHARGE% $CELLS</title>
<subtitle>$STATUS_INFO</subtitle>
<icon>icons/$BATT_ICON</icon>
<icon>$BATT_ICON</icon>
</item>
<item>
<title>$TIME_INFO</title>
<subtitle>Time Left</subtitle>
<icon>icons/clock.png</icon>
<icon>$ICON_SET/clock.png</icon>
</item>
<item>
<title>$TEMPERATURE °C</title>
<subtitle>Temperature</subtitle>
<icon>icons/temp.png</icon>
<icon>$ICON_SET/temp.png</icon>
</item>
<item>
<title>$CYCLE_COUNT</title>
<subtitle>Charge Cycles Completed</subtitle>
<icon>icons/cycles.png</icon>
<icon>$ICON_SET/cycles.png</icon>
</item>
<item>
<title>$HEALTH%</title>
<subtitle>Health</subtitle>
<icon>icons/health.png</icon>
<icon>$ICON_SET/health.png</icon>
</item>
<item>
<title>$AGE months</title>
<subtitle>Age</subtitle>
<icon>$ICON_SET/age.png</icon>
</item>
<item>
<title>$SERIAL</title>
<subtitle>Serial</subtitle>
<icon>icons/serial.png</icon>
<icon>$ICON_SET/serial.png</icon>
</item>
<item>
<title>$AGE months</title>
<subtitle>Age</subtitle>
<icon>icons/age.png</icon>
<title>$MouseTitle</title>
<subtitle>Magic Mouse</subtitle>
<icon>$MOUSE_ICON</icon>
</item>
<item>
<title>$KeyboardTitle</title>
<subtitle>Magic Keyboard</subtitle>
<icon>$KEYBOARD_ICON</icon>
</item>
<item>
<title>$TrackpadTitle</title>
<subtitle>Magic TrackPad</subtitle>
<icon>$TRACKPAD_ICON</icon>
</item>
</items>
EOB
EOB
Binary file added src/gradient-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/age.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/charging.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/clock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/critical.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/cycles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/draining.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/full.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/health.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/low.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/mouse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/power.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/serial.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/temp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/gradient-icons/trackpad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/mouse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/trackpad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.