How can I programmatically determine if my laptop is connected or not? (OSX)

In the same way as it says: I'm looking for a way, in OSX, to tell me if my laptop is currently connected, so that I can start / pause work with the CPU if necessary.

Even better, to receive notifications every time you change the state of the connected.

+4
source share
2 answers

You can use pmset:

-g ps / batt displays the status of the batteries and the UPS.

Saying:

pmset -g ps

will tell you if the laptop is running on AC power or when using a battery.

To translate into a condition, the following should work:

if [[ $(pmset -g ps | head -1) =~ "AC Power" ]]; then
  echo "power on!"
fi
+6
source

pmset(1)looks promising. In particular:

-g ps/batt .

pmset, , , , IOPSGetProvidingPowerSourceType,

, .

+2

All Articles