Last active
June 24, 2017 00:15
-
-
Save j5bot/1a37e9da3cf565275fe0993976a2c3c0 to your computer and use it in GitHub Desktop.
BitBar plugin with the aim of putting a MacBook to sleep when the battery reaches a certain percentage level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
######################### USAGE ######################### | |
# rename the plugin/script to include parameters for the | |
# minimum uptime before trying to enforce sleeping below | |
# the battery level and the battery level to sleep below | |
# use the format -<NUMBER>up-<NUMBER>perc | |
# | |
# example: sleepAt-10up-10perc.5m.sh | |
# | |
# means that BitBar will run the script every 5 minutes | |
# and the script will check whether the computer | |
# has been up for at least 10 minutes | |
# and only if it's been up for at least 10 minutes | |
# it will check whether the battery percentage | |
# is higher than 10% | |
######################################################### | |
function showUsageLink { | |
# doesn't work when the path has spaces in it | |
echo show usage'|'bash='"'"$0"'"' param1="usage" | |
} | |
echo 'sleep at' | |
echo --- | |
usage="$1" | |
if [[ $usage == "usage" ]]; then | |
echo Usage: | |
echo '' | |
echo rename the plugin/script to include parameters for the | |
echo minimum uptime before trying to enforce sleeping below | |
echo the battery level and the battery level to sleep below | |
echo use the format -\<NUMBER\>up-\<NUMBER\>perc | |
echo '' | |
echo example: sleepAt-10up-10perc.5m.sh | |
echo '' | |
echo means that BitBar will run the script every 5 minutes | |
echo and the script will check whether the computer | |
echo has been up for at least 10 minutes | |
echo and only if it\'s been up for at least 10 minutes | |
echo it will check whether the battery percentage | |
echo is higher than 10% | |
exit | |
fi | |
# check whether we're currently charging or on battery | |
powersource=$(pmset -g ps | head -n 1 | awk '{print $4 " " $5}') | |
# don't sleep when you're charging, or calculate anything | |
if [[ $powersource == "'AC Power'" ]]; then | |
echo Charging | |
showUsageLink | |
exit | |
fi | |
# all of the 'parameters' in the script name (and the interval) | |
base=$(basename "$0" | sed -e 's|^[a-zA-Z]*||') | |
# strip off the startup parameter (###up) | |
startup=$(echo "${base}" | sed -e 's|-\([0-9]*\)up.*|\1|g') | |
if [[ $startup == $base ]]; then | |
startup=20 | |
else | |
base=$(echo "${base}" | sed -e 's|-[0-9]*up\(.*\)|\1|g') | |
fi | |
# strip off the safe percentage parameter (###perc) | |
safeperc=$(echo "${base}" | sed -e 's|-\([0-9]*\)perc.*|\1|g') | |
if [[ $safeperc == $base ]]; then | |
safeperc=5 | |
else | |
base=$(echo "${base}" | sed -e 's|-[0-9]*perc\(.*\)|\1|g') | |
fi | |
# when we're not charging, show the parameters used by the script | |
echo 'minimum uptime: ' $startup | |
echo 'safe percentage: ' $safeperc | |
# echo $base | |
timeup=$(uptime) | |
upvalue=$(echo ${timeup} | awk '{print $3}') | |
upincr=$(echo ${timeup} | awk '{print $4}') | |
# don't sleep when we're just starting up ... chances are we're trying to charge | |
# and the cord's been knocked out | |
if [[ $upincr == "mins," ]]; then | |
if [ $upvalue -lt $startup ]; then | |
echo Only up for $upincr mins... | |
showUsageLink | |
exit | |
fi | |
fi | |
current="$(system_profiler SPPowerDataType | grep "Charge Remaining" | awk '{print $4}')"; | |
full="$(system_profiler SPPowerDataType | grep "Full Charge Capacity" | awk '{print $5}')"; | |
percentage=$((${current}*100/${full})) | |
unsafe="" | |
if [ ${percentage} -le ${safeperc} ]; then | |
unsafe=" < ${safeperc}, unsafe" | |
fi | |
echo "${percentage}%${unsafe}" | |
if [ ${percentage} -le ${safeperc} ]; then | |
pmset sleepnow | |
fi | |
showUsageLink | |
# <bitbar.title>Sleep At Battery %</bitbar.title> | |
# <bitbar.version>v1.0</bitbar.version> | |
# <bitbar.author>Jonathan 'J5' Cook</bitbar.author> | |
# <bitbar.author.github>j5bot</bitbar.author.github> | |
# <bitbar.desc>Sends a sleep command to the system when the battery reaches the provided percentage.</bitbar.desc> | |
# <bitbar.dependencies>none</bitbar.dependencies> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment