How can I read the battery level of a MacBook in Swift? I have seen many post here/on SO and many docs on Apple's website about reading the battery level on iOS, but what's the best way to do this on a MacBook? In the past the only way I've found to do this is with a time-consuming bash command piped to grep, which seems very inefficient. (as seen and adapted from, although I can't locate the original source I read,  here and here) Is there a better way to do this programmatically? I've also seen this, but that only appears to have an Obj-C version. Is there any way to do this in Swift?
EDIT
There's actually a better way that doesn't take any significant time (ioreg -l -w0 | grep Capacity shows current charge and maximum capacity), but it is still a seemingly unneeded bash script.
Yes, it can be done fully in Swift. You mention that you have found a sub-optimal solution for you in using the &quot;ioreg&quot; command line tool. You can find the full source code (in C) for the ioreg tool here - for inspiration:
https://github.com/opensource-apple/IOKitTools/blob/master/ioreg.tproj/ioreg.c
That tool gets the battery level for you by reading the properties of the I/O devices - the battery level being one such property. It reads them using a function that in Swift is available as IORegistryEntryCreateCFProperties() in the IOKit framework.