Skip to content

Instantly share code, notes, and snippets.

@mCzolko
Created April 23, 2023 14:27
Show Gist options
  • Save mCzolko/49fe976e56ced1bf2474705a5562e9d5 to your computer and use it in GitHub Desktop.
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)
// 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()
@mCzolko
Copy link
Author

mCzolko commented Apr 23, 2023

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment