マウス・キーボードのバッテリ残量が危ないときに通知を表示

これ。

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-30-18-39-15

homebrewとterminal-notifierを設置

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install terminal-notifier

チェックスクリプト設置

$ vim ~/battery_checker.sh
---
#!/bin/bash
BATTERY_ALERT_PERCENT=25
MAX_TRY=10
num=0

while [ $num -lt 10 ]
do
    product_name=$(ioreg -arl -c AppleDeviceManagementHIDEventService -k BatteryPercent -d 1 > /tmp/hid_info.plist \
            && /usr/libexec/PlistBuddy -c "Print $num:Product" /tmp/hid_info.plist 2>/dev/null)
    battery_percent=$(ioreg -arl -c AppleDeviceManagementHIDEventService -k BatteryPercent -d 1 > /tmp/hid_info.plist \
            && /usr/libexec/PlistBuddy -c "Print $num:BatteryPercent" /tmp/hid_info.plist 2>/dev/null)

    if [ $? -ne 0 ]; then
        break
    fi

    if [ $battery_percent -le $BATTERY_ALERT_PERCENT ]; then
        /usr/local/bin/terminal-notifier  -group "$(echo $product_name | tr " " "_")" -message "あと ${battery_percent} %" -title "${product_name} が死にそう"
    fi

    num=$(expr $num + 1)
done
---

$ chmod 755 ~/battery_checker.sh

チェックスクリプトを定期実行

$ vim ~/Library/LaunchAgents/battery_checker.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>aho.battery_checker</string>
  <key>ProgramArguments</key>
  <array>
    <string>bash</string>
    <string>/Users/develop43/battery_checker.sh</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>StartInterval</key>
  <!--2時間毎にしつこく実行-->
  <integer>7200</integer>
  <!-- for debug -->
  <!--
  <key>StandardErrorPath</key>
  <string>/tmp/battery_checker_err.log</string>
  <key>StandardOutPath</key>
  <string>/tmp/battery_checker_log.log</string>
  -->
</dict>
</plist>
---

## ジョブ登録
$ launchctl load ~/Library/LaunchAgents/battery_checker.plist

## 登録確認
$ launchctl list aho.battery_checker

辛い時

$ launchctl unload ~/Library/LaunchAgents/battery_checker.plist
$ rm ~/Library/LaunchAgents/battery_checker.plist
$ rm ~/battery_checker.sh