Skip to content

Instantly share code, notes, and snippets.

@fserafin
fserafin / check_battery.sh
Created October 20, 2024 09:54
Checks the Magic Mouse and Magic Keyboard battery on MacOS and shows a notification when it goes under a certain value
#!/bin/bash
# Set the threshold percentage (default is 20%)
THRESHOLD=${1:-20}
# Function to get battery percentage of a device
get_battery() {
local device=$1
local result=$(ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i "$device" -A 20 | grep BatteryPercent | cut -d= -f2 | tr -d '%')