LoginSignup
5
7

More than 5 years have passed since last update.

バッテリーの残量が低下したら警告してもらおう@Mac

Last updated at Posted at 2017-11-29

やりたいこと

youtubeなどで動画をフルスクリーンで見ていると、
右上に表示されているバッテリーの残量パーセンテージが見えなくなってしまい、
いつのまにかバッテリーがなくなってしまい困るのでそれを解消したい!

※電源を繋いで視聴すれば全く問題ない話ですが、
ちょくちょく外れてしまうので私はいつも外しています。

なにをやるか?

バッテリーの残量を算出するシェルスクリプトを作成し、
cronで5分間隔で動かしてチェックします。

動作確認環境

OS:macOS High Sierra 10.13.1
OS:macOS Mojave 10.14(2018/12/01追記)

動かすシェルスクリプト

下記スクリプトでは20%以下になったら警告を出すようにしています。

battery.bash
#!/bin/bash

max=$(ioreg -l | grep -F '"MaxCapacity"' | awk '$0=$NF')
now=$(ioreg -l | grep -F '"CurrentCapacity"' | awk '$0=$NF')

percentage=$(( ${now} * 100 / ${max} ))

if [ "${percentage}" -le 20 ]; then
  say "バッテリーが低下しています"
fi

exit 0

ioregについて

あまり見慣れないコマンドですがioregというコマンドを使用します。
Macには標準で入っていると思います。Linuxにはないです。
私も説明できるほど詳しくないです。。
下記、他者サイト様からの引用。

レジストリツリーの形で表現されるうえに、デバイスクラスなど開発者でも完全には把握していない情報のオンパレードで、コツを知らないかぎり使いこなすのは難しい。

MacBook Proなどに使われる内蔵バッテリーは、デバイスクラス「AppleSmartBattery」としてレジストリツリーに表示される。通常このデバイスクラス名は、Developer Toolsに付属のドキュメントを参照しなければ分からない

引用元:ASCII.jp:知ってトクするOS Xのコマンド(2) (1/2)|Apple Geeks

オプションは色々ありますが-lだけ使用します。
これが全部表示するモードっぽいです。
下記にオプションの一覧を上げておきます。
日本語が見つからなかったので、英語で記載します。すみません。

オプション 説明
-a Archive the output in XML.
-b Show the object name in bold.
-c class Show the object properties only if the object is an instance of, or derives from, the specified C++ class (e.g. IOService).
-d depth Limit tree traversal to the specified depth. The depth limit is applied with respect to each subtree root individually. Therefore, supplying a depth of 1 will cause ioreg to display only (sub)tree root nodes; children will not be shown.
-f Enable smart formatting. ioreg knows how to format certain properties so that the output is more readable and meaningful, decoding data fields where appropriate. Currently supported are 'reg', 'assigned-addresses', 'slot-names', 'ranges', 'interrupt-map', 'interrupt-parent`, and 'inter-rupts'.
-i Show the object inheritance.
-k key Show the object properties only if the object has the specified key. Substrings do not match;the specified key must be a full property name.
-l Show properties for all displayed objects.
-n name Show the object properties only if the object has the specified name. The object location, if any, is considered part of the name, thus pci@f0000000 and pci@f4000000 are distinct names.
-p place Traverse the registry over the specified place. The default plane value is "IOService". The other planes, such as "IODeviceTree", can be found under the "IORegistryPlanes" property of the root object (ioreg -d 1 -k IORegistryPlanes).
-r Show subtrees rooted by objects that match the specified criteria. If none of -c, -k, or -n are supplied, -r has no effect.
-t Show tree location of each subtree. This option causes ioreg to display all nodes between the I/O Kit Root and the root of the displayed subtree, i.e. the subtree's parent, grandparent, etc.
-w width Clip the output to the specified line width. The default width value is the current screen size.A value of 0 specifies an unlimited line width.
-x Show data and numbers as hexadecimal.

ioregを使ってみる

私の環境では下記のように表示されました。

$ ioreg -l | grep Capacity
    | |           "MaxCapacity" = 6263
    | |           "CurrentCapacity" = 3684
    | |           "LegacyBatteryInfo" = {"Amperage"=18446744073709550575,"Flags"=4,"Capacity"=6263,"Current"=3684,"Voltage"=7645,"Cycle Count"=220}
    | |           "DesignCapacity" = 7150

MaxCapacityは(多少劣化した)現在の最大容量、CurrentCapacityは現時点の容量、DesignCapacityは出荷時点でバッテリーが持っていた容量を意味する。

引用元:ASCII.jp:知ってトクするOS Xのコマンド(2) (1/2)|Apple Geeks

つまり、CurrentCapacity / MaxCapacity がバッテリー残量という訳です。
※PCの右上に表示される値と多少の誤差はあるかもしれません。

この値が特定の値以下になったときに、sayコマンドで警告を出してあげます。
このコマンドは本当に音声が出るので使用する場所は選んでください。

デフォルトの設定では日本人のKyokoさんがしゃべってくれます。

このコマンドは私もまだ使いこなせていませんが、
いいおもちゃになってくれそうです。

sayコマンドの使用方法とcronの設定方法も書こうかなと思いましたが、
このページが多くなってしまうのと、他に素晴らしいサイト様がたくさんありますので
そちらに譲ります。

参考にさせて頂いたサイト様

ASCII.jp:知ってトクするOS Xのコマンド(2) (1/2)|Apple Geeks
ioreg(8) Mac OS X Manual Page
Macのsayコマンドの使い方 - Qiita

5
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7