Skip to content

Instantly share code, notes, and snippets.

@jessebutryn
Last active March 3, 2018 23:08
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 jessebutryn/4a0277a4986549974215ac46a744be5d to your computer and use it in GitHub Desktop.
Save jessebutryn/4a0277a4986549974215ac46a744be5d to your computer and use it in GitHub Desktop.
Mac notify script
#!/usr/bin/env bash
#set -x
#
# Copyright (c) 2018 Joyent Inc.
#
# This script will set a reminder on your mac that generates a popup notification
# using applescript upon completion.
#
# shellcheck disable=SC2155
#
# Author: Jesse Butryn <jesse.butryn@joyent.com>
#
# 03/01/2018 - Initial draft.
#
##############################
# Table of Contents
##############################
#
# 1) Functions
# 2) Arguments
# 3) Run
#
##############################
# Section 1 Functions
##############################
echo.help () { # Displays help and usage information
cat <<EOF
This script will set a reminder that sends a popup notification to your mac.
Usage: $(basename "$0") +0130 -m Dont forget that thing you need not forget.
$(basename "$0") [[+]HHMM] [-m message]
HHMM The time of day is in the form hhmm where HH is a time in hours (24 hour clock only), and MM are minutes.
+ If the time is preceded by '+', the alarm will go off in hours and minutes from the current time.
-h Print help output.
-m (Optional) Enter message that will appear in the popup
EOF
exit 10
} # end echo.help
mac.notify () {
local okay
local cancel
local message
while (( $# )); do
case $1 in
-o) shift; okay=$1;;
-c) shift; cancel=$1;;
*) message=$*;break;;
esac
shift
done
osascript <<EOF
set theDialogText to "${message:-"Alert!"}"
display dialog theDialogText buttons {"${cancel:-"Also Okay"}", "${okay:-Okay}"} default button "${okay:-Okay}" cancel button "${cancel:-"Also Okay"}"
EOF
}
calc.offset () {
local h=${1:0:2}
local m=${1:2:2}
date -v+"${h}H" -v+"${m}M"
}
sleep.offset () {
local h=${1:0:2}
local m=${1:2:2}
local s=$(( (h*60*60) + (m*60) ))
echo "$s"
}
sleep.time () {
local t=$1
local cur_epoch=$(date '+%s')
local end_epoch=$(date -j -f '%H%M' "$t" '+%s')
local s=$((end_epoch-cur_epoch))
echo "$s"
}
notify.main () {
echo -e "Timer set for:\t$end_time"
sleep "$sleep_time" && mac.notify "$notify_mesg" >/dev/null 2>&1 &
}
##############################
# Section 2 Arguments
##############################
[[ "$#" -lt '1' ]] && echo.help
while (( $# )); do
case $1 in
-m) shift; notify_mesg=$*;break;;
+[0-9][0-9][0-9][0-9])
sleep_time=$(sleep.offset "${1/+/}")
end_time=$(calc.offset "${1/+/}")
;;
[0-9][0-9][0-9][0-9])
sleep_time=$(sleep.time "$1")
end_time=$(date -j -f '%H%M' "$1")
;;
*) echo.help;;
esac
shift
done
##############################
# Section 3 Run
##############################
notify.main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment