Skip to content

Commit

Permalink
detect apps preventing sleep
Browse files Browse the repository at this point in the history
improve detection of apps preventing sleep or display sleep
  • Loading branch information
glowinthedark authored Mar 25, 2023
1 parent c2d9018 commit 780aebd
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions System/pmset.5m.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#!/usr/bin/env bash
# <bitbar.title>Sleep Manager</bitbar.title>
# <bitbar.version>v0.3</bitbar.version>
# <bitbar.author>glowinthedark</bitbar.author>
# <bitbar.author.github>glowinthedark</bitbar.author.github>
# <bitbar.desc>Sleeping and power management GUI. View current sleep status, disable sleep on battery, prevent sleeping using ootb caffeinate</bitbar.desc>
# <bitbar.image>https://telegra.ph/file/20ef9918b679e238bc5ff.png</bitbar.image>
# <bitbar.abouturl>https://github.com/matryer/xbar-plugins/blob/main/System/pmset.5m.sh</bitbar.abouturl>

if [[ "$1" = "pmset_disable_battery_sleep" ]]; then
osascript -e 'display dialog "Disable system sleep while on battery?" buttons {"Cancel", "DISABLE NOW! (System will NOT sleep!)"}
Expand All @@ -23,21 +30,34 @@ if [[ "$1" = "start_caffeinate" ]]; then
fi

if [[ "$1" = "edit_this_script" ]]; then
open -b com.sublimetext.3 "$0"
# open with default editor registered for .sh extension
open "$0";
# OR use an explicit editor by app name or by bundle
# open -a 'Sublime Text' "$0"
# open -b com.sublimetext.4 "$0"
exit
fi

sleep_disabled=$(pmset -g | grep SleepDisabled | awk '{print $2}')
battery_stats="$(pmset -g)"
sleep_disabled=$(echo "$battery_stats" | awk '$1~/SleepDisabled/ {print $2}')
sleep_prevented=$(echo "$battery_stats" | awk -F'(' '$0~/\(sleep prevented/ {print $2}' | tr -d ')')
display_sleep_prevented=$(echo "$battery_stats" | awk -F'(' '$0~/\(display sleep prevented/ {print $2}' | tr -d ')')

is_caffeinate_running=$(pgrep caffeinate)
pgrep -n caffeinate -q &>/dev/null
is_caffeinate_running="$?"

if [[ $sleep_disabled == "0" ]]; then
if [[ $is_caffeinate_running -eq 0 ]] ; then # "1" = no processes found
echo "🔵"
status="💤 Sleeping normally"
else
echo "☕️"
status="caffeinating... ☕️"
else
if [[ "_$sleep_prevented" != "_" ]] ; then
echo '♨️'
status="♨️ Sleep prevented by application(s):"
else
echo "🔋"
status="💤 Sleeping normally"
fi
fi
cmd="🔋 pmset: Disable sleep on battery | bash='$0' param1=pmset_disable_battery_sleep terminal=false refresh=true"
else
Expand All @@ -48,13 +68,21 @@ fi

echo '---'
echo "$status"
if [[ "_$sleep_prevented" != "_" ]] ; then
echo '---'
echo "❗️$sleep_prevented"
fi
if [[ "_$display_sleep_prevented" != "_" ]] ; then
echo '---'
echo "‼️$display_sleep_prevented"
fi
echo '---'
echo "$cmd"

if [[ $is_caffeinate_running -eq 0 ]] ; then
echo "☕️ caffeinate | bash='$0' param1=start_caffeinate refresh=true terminal=false"
echo "❌ DECAFFEINATE! | bash='$0' color=indianred param1=stop_caffeinate refresh=true terminal=false"
else
echo "❌ [caffeinate already running] KILL NOW | bash='$0' color=indianred param1=stop_caffeinate refresh=true terminal=false"
echo "☕️ caffeinate | bash='$0' param1=start_caffeinate refresh=true terminal=false"
fi
echo '---'
echo "✏️ Edit this file | bash='$0' param1="edit_this_script" terminal=false"
Expand Down

0 comments on commit 780aebd

Please sign in to comment.