Skip to content

Instantly share code, notes, and snippets.

@joshbetz

joshbetz/track

Created Oct 21, 2014
Embed
What would you like to do?
#!/bin/bash
trap stop SIGINT SIGHUP
usage() {
track=`basename $0`
echo "usage: $track <time> <name> [--quiet]"
}
# Validate usage
if [ $# -lt 2 ]; then
usage
exit 1
fi
# Parse command line flags
TIME=$1
NAME=$2
QUIET=false
shift 2
for i in "$@"
do
case $i in
--quiet)
QUIET=true
;;
*)
usage
exit 1
;;
esac
done
notify() {
/usr/bin/osascript -e "display notification \"$1\" sound name \"Glass\" with title \"track(1)\""
}
log() {
local NOW=`date +"%F %T"`
echo "[$NOW] $1" >> $HOME/.track
}
cleanup() {
log '--done'
toggleDND
}
stop() {
cleanup
exit
}
toggleDND() {
$QUIET && /usr/bin/osascript -e "tell application \"System Events\"
option key down
try
click menu bar item \"Notification Center\" of menu bar 2 of application process \"SystemUIServer\"
on error error_message number error_number
if error_number is -1719 then
display alert error_message
end if
end try
try
click menu bar item \"NotificationCenter, Do Not Disturb enabled\" of menu bar 2 of application process \"SystemUIServer\"
on error error_message number error_number
if error_number is -1719 then
display alert error_message
end if
end try
option key up
end tell"
}
countdown() {
min=`expr $1 / 60`
sec=`expr $1 % 60`
while [ $min -ge 0 ]; do
while [ $sec -ge 0 ]; do
printf "%02d:%02d\033[0K\r" $min $sec
sec=$((sec-1))
sleep 1
done
sec=59
min=$((min-1))
done
cleanup
notify $2
}
# Go
toggleDND
log $NAME
countdown $TIME $NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment