Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ######################### USAGE #########################
  4. # rename the plugin/script to include parameters for the
  5. # minimum uptime before trying to enforce sleeping below
  6. # the battery level and the battery level to sleep below
  7. # use the format -<NUMBER>up-<NUMBER>perc
  8. #
  9. # example: sleepAt-10up-10perc.5m.sh
  10. #
  11. # means that BitBar will run the script every 5 minutes
  12. # and the script will check whether the computer
  13. # has been up for at least 10 minutes
  14. # and only if it's been up for at least 10 minutes
  15. # it will check whether the battery percentage
  16. # is higher than 10%
  17. #########################################################
  18.  
  19. function showUsageLink {
  20.  
  21. echo show usage'|'bash='"'"$0"'"' param1="usage"
  22.  
  23. }
  24.  
  25. echo 'sleep at'
  26. echo ---
  27.  
  28. usage="$1"
  29.  
  30. if [[ $usage == "usage" ]]; then
  31.  
  32. echo Usage:
  33. echo ''
  34. echo rename the plugin/script to include parameters for the
  35. echo minimum uptime before trying to enforce sleeping below
  36. echo the battery level and the battery level to sleep below
  37. echo use the format -\<NUMBER\>up-\<NUMBER\>perc
  38. echo ''
  39. echo example: sleepAt-10up-10perc.5m.sh
  40. echo ''
  41. echo means that BitBar will run the script every 5 minutes
  42. echo and the script will check whether the computer
  43. echo has been up for at least 10 minutes
  44. echo and only if it\'s been up for at least 10 minutes
  45. echo it will check whether the battery percentage
  46. echo is higher than 10%
  47.  
  48. exit
  49. fi
  50.  
  51. # check whether we're currently charging or on battery
  52. powersource=$(pmset -g ps | head -n 1 | awk '{print $4 " " $5}')
  53.  
  54. # don't sleep when you're charging, or calculate anything
  55. if [[ $powersource == "'AC Power'" ]]; then
  56. echo Charging
  57. showUsageLink
  58. exit
  59. fi
  60.  
  61. # all of the 'parameters' in the script name (and the interval)
  62. base=$(basename "$0" | sed -e 's|^[a-zA-Z]*||')
  63.  
  64. # strip off the startup parameter (###up)
  65. startup=$(echo "${base}" | sed -e 's|-\([0-9]*\)up.*|\1|g')
  66. if [[ $startup == $base ]]; then
  67. startup=20
  68. else
  69. base=$(echo "${base}" | sed -e 's|-[0-9]*up\(.*\)|\1|g')
  70. fi
  71.  
  72. # strip off the safe percentage parameter (###perc)
  73. safeperc=$(echo "${base}" | sed -e 's|-\([0-9]*\)perc.*|\1|g')
  74. if [[ $safeperc == $base ]]; then
  75. safeperc=5
  76. else
  77. base=$(echo "${base}" | sed -e 's|-[0-9]*perc\(.*\)|\1|g')
  78. fi
  79.  
  80. # when we're not charging, show the parameters used by the script
  81. echo 'minimum uptime: ' $startup
  82. echo 'safe percentage: ' $safeperc
  83.  
  84. # echo $base
  85.  
  86. timeup=$(uptime)
  87. upvalue=$(echo ${timeup} | awk '{print $3}')
  88. upincr=$(echo ${timeup} | awk '{print $4}')
  89.  
  90. # don't sleep when we're just starting up ... chances are we're trying to charge
  91. # and the cord's been knocked out
  92. if [[ $upincr == "mins," ]]; then
  93. if [ $upvalue -lt $startup ]; then
  94. echo Only up for $upincr mins...
  95. showUsageLink
  96. exit
  97. fi
  98. fi
  99.  
  100. current="$(system_profiler SPPowerDataType | grep "Charge Remaining" | awk '{print $4}')";
  101. full="$(system_profiler SPPowerDataType | grep "Full Charge Capacity" | awk '{print $5}')";
  102.  
  103. percentage=$((${current}*100/${full}))
  104. unsafe=""
  105.  
  106. if [ ${percentage} -le ${safeperc} ]; then
  107. unsafe=" < ${safeperc}, unsafe"
  108. fi
  109.  
  110. echo "${percentage}%${unsafe}"
  111.  
  112. if [ ${percentage} -le ${safeperc} ]; then
  113. pmset sleepnow
  114. fi
  115.  
  116. showUsageLink
  117.  
  118. # <bitbar.title>Sleep At Battery %</bitbar.title>
  119. # <bitbar.version>v1.0</bitbar.version>
  120. # <bitbar.author>Jonathan 'J5' Cook</bitbar.author>
  121. # <bitbar.author.github>j5bot</bitbar.author.github>
  122. # <bitbar.desc>Sends a sleep command to the system when the battery reaches the provided percentage.</bitbar.desc>
  123. # <bitbar.dependencies>none</bitbar.dependencies>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement