Check the current power source type (Battery or AC/UPS).
powerstatus is a command-line tool written in Swift to determine the current power source of a macOS device (battery, AC, or UPS) and exit with a corresponding status code.
You can either download a precompiled binary or compile it yourself.
The precompiled binary is a universal build, compatible with both Intel and Apple Silicon Macs. To download it, run:
curl --fail --silent --show-error --location --output-dir bin --create-dirs --remote-name --url "https://github.com/toobuntu/powerstatus/releases/latest/download/powerstatus"
# It’s not signed or notarized, so remove any potential runtime issues
if xattr -p com.apple.quarantine bin/powerstatus 2>/dev/null; then
xattr -d com.apple.quarantine bin/powerstatus
fiInstall to your PATH (e.g., /usr/local/bin):
# Install to your PATH for global access
sudo install -m 0755 bin/powerstatus /usr/local/bin/
# Run it from anywhere
powerstatus --helpOr, run it directly from the bin directory:
# Make it executable
chmod +x bin/powerstatus
# Run directly from the bin directory
./bin/powerstatus --helpIf you’d rather compile the binary yourself, first clone this repository and ensure the Xcode Command Line Tools are installed. Then run the provided build script:
ksh ./scripts/build.shThis script will build powerstatus.swift twice:
- Per architecture with debug symbols included.
- As a stripped universal binary for distribution.
After running the script, you will find the distribution build in the ./build/dist/ directory and the universal binary in the ./bin/ directory.
And that’s it! You’re all set to use powerstatus. There’s no need to code sign or notarize software for it to run on the machine where it was compiled.
Once compiled, you can either run it directly from the bin directory or install it to a directory in your PATH and run it from anywhere:
# Run the universal binary directly from the bin directory
./bin/powerstatus --help
# Or, run a per-architecture build from the dist directory
./build/dist/powerstatus_arm64 --helpOr, install it to a directory in your PATH (e.g., /usr/local/bin) to run it from anywhere:
# Install the universal binary
sudo install -m 0755 ./bin/powerstatus /usr/local/bin/
# Install a per-architecture build and rename it during installation
# For Intel Macs, replace arm64 with x86_64
sudo install -m 0755 ./build/dist/powerstatus_arm64 /usr/local/bin/powerstatus
# Now you can run it from anywhere
powerstatus --helpThe Swift compiler is provided by Xcode Command Line Tools, which you can install with:
xcode-select --installpowerstatus [options]
Options:
-v, --verbose Enable verbose output
--debug Enable debug output
-h, --help Show this help message
Exit Codes:
0: Device is running on battery.
1: Device is plugged into AC.
2: Device is plugged into UPS.This command-line utility supports macOS 13 (Ventura) and later. Note that testing on macOS versions earlier than 13 is no longer feasible as GitHub runners only support macOS 13 and later.
The program relies on the IOKit framework, specifically using IOPSCopyPowerSourcesInfo and IOPSGetProvidingPowerSourceType functions. These functions have been available since macOS 10.0. However, considering modern Swift features and compiling requirements, the minimum macOS version that can be targeted is macOS 10.12 (Sierra).
For detailed build instructions on older macOS versions, refer to the BUILD_LEGACY_MACOS.md file.
For debugging instructions, refer to the DEBUGGING.md file.
- You can star ⭐️ this project on GitHub
- Or share the program on social networks
👤 Todd Schulman
- Github: @toobuntu
Contributions are welcome. Please open an issue or submit a pull request for bug reports and feature requests. For major changes, please open an issue first to discuss what you would like to change.
Small note: If editing the README, please conform to the standard-readme specification.
This project is available under your choice of the following licenses:
- Apache-2.0
- BSD-2-Clause
- GPL-3.0-or-later
You may select any of those, or use a combination of them. The default license is Apache-2.0, in accordance with GNU guidelines for small programs with fewer than 300 lines of code.