Skip to content

Instantly share code, notes, and snippets.

@GiriB
Created May 7, 2016 11:03
Show Gist options
  • Save GiriB/e6ddc9f753e1513f9c2666c9f32f8911 to your computer and use it in GitHub Desktop.
Save GiriB/e6ddc9f753e1513f9c2666c9f32f8911 to your computer and use it in GitHub Desktop.
Get Battery Low Notifications in the Mac OSX Notification Center
#!/usr/local/bin/python
# author : Giri Gaurav Bhatnagar
# email : giri1gb@gmail.com
# Sends a notification in the Notification Center
# when Battery is less than THRESHOLD value
# Needs terminal-notifier : https://github.com/julienXX/terminal-notifier
import os,time
# Notify when battery is less than THRESHOLD
THRESHOLD = 40
#Keep polling baterry status after TIME seconds
TIME = 600
while True:
p = os.popen("pmset -g batt | grep -o '[0-9]\{1,3\}'%","r")
battery = int(p.read().strip()[:2])
if (battery <= THRESHOLD):
# send notification
os.system('terminal-notifier -message "IT\'S TIME TO CHARGE! " -title "BatteryNotify"')
time.sleep(TIME);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment