Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

powerstatus

standard-readme compliant License: Apache--2.0 OR BSD--2--Clause OR GPL--3.0--or--later GitHub commits since latest release GitHub last commit Endpoint Badge GitHub Release Date GitHub Created At

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.

Install

You can either download a precompiled binary or compile it yourself.

Download

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
fi

Install 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 --help

Or, run it directly from the bin directory:

# Make it executable
chmod +x bin/powerstatus

# Run directly from the bin directory
./bin/powerstatus --help

Compile

If 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.sh

This 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 --help

Or, 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 --help

Build dependencies

The Swift compiler is provided by Xcode Command Line Tools, which you can install with:

xcode-select --install

Usage

powerstatus [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.

Compatibility

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.

If You Want To Thank Me

  • You can star ⭐️ this project on GitHub
  • Or share the program on social networks

Maintainer

👤 Todd Schulman

Contributing

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.

License

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.

About

Check the current power source type (Battery or AC/UPS) on macOS

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages