Skip to content

Instantly share code, notes, and snippets.

@marksowell
Last active September 3, 2023 03:01
Show Gist options
  • Save marksowell/be3690ce80d55cd0b8a73968e5cdde04 to your computer and use it in GitHub Desktop.
Save marksowell/be3690ce80d55cd0b8a73968e5cdde04 to your computer and use it in GitHub Desktop.
Hammerspoon macOS Battery Percentage <=10% Alert
local alertShown = false
function checkBatteryStatus()
local currentCapacitymAh = hs.battery.capacity()
local maxCapacitymAh = hs.battery.maxCapacity()
local currentCapacityPercentage = (currentCapacitymAh / maxCapacitymAh) * 100
local isCharging = hs.battery.isCharging()
if currentCapacityPercentage <= 5 and not alertShown then
hs.alert.show("Battery capacity <= 5%!", 500.0) -- 500 seconds duration for the alert
alertShown = true
elseif currentCapacityPercentage > 5 or isCharging then
hs.alert.closeAll() -- Close the alert if capacity is more than 10% or if power is connected
alertShown = false
end
end
batteryWatcher = hs.battery.watcher.new(checkBatteryStatus)
batteryWatcher:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment