Skip to content

Instantly share code, notes, and snippets.

@silasjmatson
Created May 13, 2017 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silasjmatson/0b7cc698955ccb8f9d48360fc7ff7e76 to your computer and use it in GitHub Desktop.
Save silasjmatson/0b7cc698955ccb8f9d48360fc7ff7e76 to your computer and use it in GitHub Desktop.
A simple zshell egg timer that runs a timer in a detached process and posts a Notification to Apple's Notification Center
# Usage: countdown <n> [second(s)|minute(s)|hour(s)] [message]
# Be sure to quote the 'message' argument
function countdown(){
if [ -z $1 ]; then
echo "Fuzzy Wuzzy wuz a bear, but now he's a sad \
bear because you didn't tell him how long to wait. 🐻"
return 1
fi
if [ -z $2 ]; then
_TIME_UNIT="minutes"
else
_TIME_UNIT="$2"
fi
if [ -z $3 ]; then
_MESSAGE="Whee, break time!"
else
_MESSAGE="$3"
fi
# Variable SCOPEZ!
_MULTIPLIER=
case $_TIME_UNIT in
"hours"|"hour") _MULTIPLIER=3600;;
"minutes"|"minute") _MULTIPLIER=60;;
"seconds"|"second") _MULTIPLIER=1;;
*) echo "Invalid Unit, assuming you meant minutes" && _MULTIPLIER=60;;
esac
_TIME_FROM_NOW=$(($1 * $_MULTIPLIER))
(
_DATE=$((`date +%s` + $_TIME_FROM_NOW));
while [ "$_DATE" -ge `date +%s` ]; do
sleep 1
done
osascript -e 'on run {message}' \
-e 'display notification message as text with title "Countdown Finished" sound name "Submarine"' \
-e 'end run' $_MESSAGE
) &!
echo "Counting down for $1 $_TIME_UNIT"
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment