Skip to content

Instantly share code, notes, and snippets.

@nolenroyalty
Created November 23, 2023 05:47
Show Gist options
  • Save nolenroyalty/5c27c7bea201806bf35a99d79debc280 to your computer and use it in GitHub Desktop.
Save nolenroyalty/5c27c7bea201806bf35a99d79debc280 to your computer and use it in GitHub Desktop.
A script I run via cron that prompts me to sleep at night
#!/usr/bin/env bash -x
# config file looks like
#START_TIME:0.16
#END_TIME:6.0
#HUSHED_UNTIL:2023-11-23T1:40:00
if [ "$1" = "-from-cron" ]
then
if ps aux | grep osascript | grep -q 'Time to Sleep'
then
echo There is probably a pending window, exiting
exit 0
fi
fi
alert_title="Time to Sleep"
alert_message=$(
cat <<'EOF'
Most of the time when you are using your computer at this hour you aren't doing anything worthwhile.
If you're programming or doing something else cool, edit ~/.go-to-bed to hush this message until tomorrow.
Otherwise you should consider sleeping or doing something more fun! Read a book or a new yorker article. Play a game. Play your piano.
EOF
)
start_time=$(cat ~/.go-to-bed | grep START_TIME | cut -d: -f2)
end_time=$(cat ~/.go-to-bed | grep END_TIME | cut -d: -f2)
time=$(date +%H.%M)
convert_to_minutes() {
hours=$(echo $1 | cut -d. -f1)
minutes=$(echo $1 | cut -d. -f2)
echo $((10#$hours * 60 + 10#$minutes))
}
# Convert HH:MM to minutes
start_minutes=$(convert_to_minutes $start_time)
end_minutes=$(convert_to_minutes $end_time)
current_minutes=$(convert_to_minutes $time)
if (( $current_minutes >= $start_minutes && $current_minutes < $end_minutes ))
then
echo in window, maybe notifying
hush_time=$(cat ~/.go-to-bed | grep HUSHED_UNTIL | cut -d: -f2-)
zone=$(date +%z)
sec_hush=$(date -j -f '%Y-%m-%dT%H:%M:%S%z' "${hush_time}${zone}" +"%s")
sec_now=$(date +%s)
if (( $sec_hush > $sec_now ))
then
echo hushed, doing nothing
else
echo notifiying
afplay /System/Library/Sounds/Blow.aiff
say -v Daniel time to sleep
osascript -e "display alert \"${alert_title}\" message \"${alert_message}\""
fi
fi
@wongjustin99
Copy link

wongjustin99 commented Dec 24, 2023

Neat script! Tweaked it a little bit so the audio wouldn't block the popup:

        afplay /System/Library/Sounds/Blow.aiff &
        sleep 0.5 && say -v Daniel time to sleep &
        osascript -e "display alert \"${alert_title}\" message \"${alert_message}\""

@nolenroyalty
Copy link
Author

@wongjustin99 thanks!! that seems better although I'm so used to the old behavior idk if I can switch!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment