Battery levels (utf8) widget screenshot

Script

Battery levels (utf8)

by Lari Kovanen

This is a rewrite of Battery levels by Wayne Folta to handle international characters. Waynes original text below.
The Apple Batteries widget can only show 4 things (in Small and Medium size) and you exceed that if you use an Apple Magic Keyboard (1), Apple Magic Trackpad (1), Apple Airpods (2) and your MacBook Pro itself (1). So I made this widget that is plain text, but does the job. I run it from cron every 15 minutes.

Place an image in ~/Pictures/battery-bg.jpg for a background image.

Set the target widget target in TW_TARGET

Script (widget-batteries-utf8.sh)

#!/bin/bash
TW=/Applications/TerminalWidget.app/Contents/MacOS/TerminalWidget
BG_IMAGE="$HOME/Pictures/battery-bg.jpg"   # any local photo
TW_TARGET="batteries"

raw=$(/usr/bin/pmset -g accps \
  | LC_ALL=C /usr/bin/tr '\325' "'" \
  | /usr/bin/iconv -f MACROMAN -t UTF-8)

case "$raw" in
  *"'AC Power'"*) title=":electric_plug: On AC Power" ;;
  *)              title=":battery: On Battery Power" ;;
esac

printf '%s\n' "$raw" \
| /usr/bin/awk -F '\t' '
    /^Now drawing from/ { next }
    NF >= 2 {
      name = $1
      sub(/^[[:space:]]*-/, "", name)
      sub(/[[:space:]]*\(id=[^)]*\)[[:space:]]*$/, "", name)
      if (name ~ /^InternalBattery/) name = "This Mac"
      pct = $2
      sub(/%.*/, "", pct)
      pct += 0
      filled = int((pct + 5) / 10)
      meter = ""
      for (i = 1; i <= 10; i++) meter = meter (i <= filled ? "▰" : "▱")
      rows = rows name "\t" meter "\t" pct "%\n"
    }
    END { printf "Device\tCharge\tLevel\n%s", rows }
  ' \
| "$TW" --target $TW_TARGET \
    --table - \
    --grid zebra-row \
    --table-layout fill \
    --table-align left,left,right \
    --title "$title" \
    --background-image "$BG_IMAGE" \
    --filter alpha:30 \
    --bg "#0f172a" \
    --fg "#e2e8f0"

Download script

Running in the background with launchd

Suggested interval: 15 minutes (StartInterval = 900 seconds).

Save the script from this recipe to ~/bin/battery-levels-utf8.sh, then create a Launch Agent plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.terminalwidget.battery-levels-utf8</string>
  <key>ProgramArguments</key>
  <array>
    <string>/bin/bash</string>
    <string>~/bin/battery-levels-utf8.sh</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>StartInterval</key>
  <integer>900</integer>
  <key>StandardOutPath</key>
  <string>/tmp/com.terminalwidget.battery-levels-utf8.log</string>
  <key>StandardErrorPath</key>
  <string>/tmp/com.terminalwidget.battery-levels-utf8.err</string>
</dict>
</plist>

From Terminal:

mkdir -p ~/bin ~/Library/LaunchAgents
# Save your script to ~/bin/battery-levels-utf8.sh
cat > ~/Library/LaunchAgents/com.terminalwidget.battery-levels-utf8.plist <<'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.terminalwidget.battery-levels-utf8</string>
  <key>ProgramArguments</key>
  <array>
    <string>/bin/bash</string>
    <string>~/bin/battery-levels-utf8.sh</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>StartInterval</key>
  <integer>900</integer>
  <key>StandardOutPath</key>
  <string>/tmp/com.terminalwidget.battery-levels-utf8.log</string>
  <key>StandardErrorPath</key>
  <string>/tmp/com.terminalwidget.battery-levels-utf8.err</string>
</dict>
</plist>
EOF
launchctl bootstrap "gui/$(id -u)" ~/Library/LaunchAgents/com.terminalwidget.battery-levels-utf8.plist

For a GUI editor and troubleshooting, see LaunchControl from soma-zone.

← All recipes