Skip to main content
deleted 192 characters in body
Source Link

So I just registered and have not enough Kharma to comment on the original post (but I can ask a new question and this makes no sense to me; go figure), but I got this excellent script from Will Cain from here (Get low battery notifications for mouse earlier), and I modified it a little.

The intention is to keep the battery between 20 and 80%, but I don't want the messages to keep appearing, for example, if the battery is lower then 20% but is already charging; or if the battery is highter then 80% and it's not charging anymore.

I'm pretty noob when it comes to bash scripting, though. But here's the general idea:

   #!/usr/bin/env bash
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin

# actual battery level
BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i keyboard -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`

#Devices is connected, not charging
CONN=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i connected - A 20`

#Device is charging
CHARG=`ioreg -cp AppleDeviceManagementHIDEventServiceIOUSB -rl -lw 0 | grep -i charging -A"magic 20`keyboard"`

# defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
MIN=${1:-20}

# defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
MAX=${1:-80}

if [ -z "$BATT" ]; then
  echo 'No keyboard found.'
  exit 0
fi

if ((( BATT < MIN )) && ((CONN))); then
  osascript -e "display notification \"Keyboard battery is at ${BATT}%.\" with title \"Keyboard Battery Low\""
fi

if ((( BATT > MAX )) && ((CHARG != null))); then
  osascript -e "display notification \"Keyboard battery is at ${BATT}%.\" with title \"Keyboard Battery High\""
fi

Obviously(Updated to reflect last version, as suggested by @mspasov in a comment, but which throws a syntax error at line 1125 (CHARGthe last "if") doesn't work because ioreg doesn't seem to contain that information. Any suggestions, maybe something more elegant then this?

So I just registered and have not enough Kharma to comment on the original post (but I can ask a new question and this makes no sense to me; go figure), but I got this excellent script from Will Cain from here (Get low battery notifications for mouse earlier), and I modified it a little.

The intention is to keep the battery between 20 and 80%, but I don't want the messages to keep appearing, for example, if the battery is lower then 20% but is already charging; or if the battery is highter then 80% and it's not charging anymore.

I'm pretty noob when it comes to bash scripting, though. But here's the general idea:

   #!/usr/bin/env bash
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin

# actual battery level
BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i keyboard -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`

#Devices is connected, not charging
CONN=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i connected - A 20`

#Device is charging
CHARG=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i charging -A 20`

# defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
MIN=${1:-20}

# defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
MAX=${1:-80}

if [ -z "$BATT" ]; then
  echo 'No keyboard found.'
  exit 0
fi

if ((( BATT < MIN )) && ((CONN))); then
  osascript -e "display notification \"Keyboard battery is at ${BATT}%.\" with title \"Keyboard Battery Low\""
fi

if ((( BATT > MAX )) && ((CHARG))); then
  osascript -e "display notification \"Keyboard battery is at ${BATT}%.\" with title \"Keyboard Battery High\""
fi

Obviously line 11 (CHARG) doesn't work because ioreg doesn't seem to contain that information. Any suggestions, maybe something more elegant then this?

So I just registered and have not enough Kharma to comment on the original post (but I can ask a new question and this makes no sense to me; go figure), but I got this excellent script from Will Cain from here (Get low battery notifications for mouse earlier), and I modified it a little.

The intention is to keep the battery between 20 and 80%, but I don't want the messages to keep appearing, for example, if the battery is lower then 20% but is already charging; or if the battery is highter then 80% and it's not charging anymore.

I'm pretty noob when it comes to bash scripting, though. But here's the general idea:

#!/usr/bin/env bash
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin

# actual battery level
BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i keyboard -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`

#Device is charging
CHARG=`ioreg -p IOUSB -l -w 0 | grep -i "magic keyboard"`

# defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
MIN=${1:-20}

# defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
MAX=${1:-80}

if [ -z "$BATT" ]; then
  echo 'No keyboard found.'
  exit 0
fi

if (( BATT < MIN )); then
  osascript -e "display notification \"Keyboard battery is at ${BATT}%.\" with title \"Keyboard Battery Low\""
fi

if ((( BATT > MAX )) && ((CHARG != null))); then
  osascript -e "display notification \"Keyboard battery is at ${BATT}%.\" with title \"Keyboard Battery High\""
fi

(Updated to reflect last version, as suggested by @mspasov in a comment, but which throws a syntax error at line 25 (the last "if").

Source Link

How to tell when Magic Keyboard or Trackpad are charging from bash

So I just registered and have not enough Kharma to comment on the original post (but I can ask a new question and this makes no sense to me; go figure), but I got this excellent script from Will Cain from here (Get low battery notifications for mouse earlier), and I modified it a little.

The intention is to keep the battery between 20 and 80%, but I don't want the messages to keep appearing, for example, if the battery is lower then 20% but is already charging; or if the battery is highter then 80% and it's not charging anymore.

I'm pretty noob when it comes to bash scripting, though. But here's the general idea:

   #!/usr/bin/env bash
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin

# actual battery level
BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i keyboard -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`

#Devices is connected, not charging
CONN=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i connected - A 20`

#Device is charging
CHARG=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i charging -A 20`

# defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
MIN=${1:-20}

# defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
MAX=${1:-80}

if [ -z "$BATT" ]; then
  echo 'No keyboard found.'
  exit 0
fi

if ((( BATT < MIN )) && ((CONN))); then
  osascript -e "display notification \"Keyboard battery is at ${BATT}%.\" with title \"Keyboard Battery Low\""
fi

if ((( BATT > MAX )) && ((CHARG))); then
  osascript -e "display notification \"Keyboard battery is at ${BATT}%.\" with title \"Keyboard Battery High\""
fi

Obviously line 11 (CHARG) doesn't work because ioreg doesn't seem to contain that information. Any suggestions, maybe something more elegant then this?