Created
April 23, 2023 14:27
-
-
Save mCzolko/49fe976e56ced1bf2474705a5562e9d5 to your computer and use it in GitHub Desktop.
Magic Battery - Get the battery percentage of your Apple Magic devices (Keyboard, Trackpad)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install ioreg --save | |
const ioreg = require('ioreg') | |
async function getExternalDevicesBatteryPercent() { | |
const devices = await ioreg('AppleDeviceManagementHIDEventService') | |
const externalDevices = devices.filter(device => !!device['BatteryPercent']) | |
return externalDevices.map(device => ([ device['Product'], device['BatteryPercent'] ])) | |
} | |
async function main() { | |
const batteryLevels = await getExternalDevicesBatteryPercent() | |
batteryLevels.forEach((batteryLevel) => { | |
console.log(batteryLevel[1] + '% - ' + batteryLevel[0]) | |
}) | |
} | |
main() |
Author
mCzolko
commented
Apr 23, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment