From fd5ec73a1b6fc14070f91a6480b9ad269ab6c797 Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Tue, 18 Oct 2022 15:55:54 +0200 Subject: [PATCH] Add notify tool --- shell/notify | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 shell/notify diff --git a/shell/notify b/shell/notify new file mode 100755 index 0000000..4e7d29b --- /dev/null +++ b/shell/notify @@ -0,0 +1,92 @@ +function notify() { + local title + local message + local subtitle + local sound + local command + local rc + + command=() + + while [[ "${#}" -gt 0 ]]; do + case "${1}" in + -t|--title) + title="${2}" + shift + ;; + -s|--subtitle) + subtitle="${2}" + shift + ;; + -m|--message) + message="${2}" + shift + ;; + -S|--sound) + if [[ -z "${2:-}" || "${2}" == '-*' ]]; then + sound='1' + else + sound="${2}" + shift + fi + ;; + --) + shift + command=("${@}") + break + ;; + *) + command=("${@}") + break + ;; + esac + + shift + done + + if [[ "${#command[@]}" -gt 0 ]]; then + "${command[@]}" + rc="${?}" + fi + + if [[ -z "${title}" && "${#command[@]}" -gt 0 ]]; then + title="${command[*]}" + fi + + if [[ -z "${message}" && "${#command[@]}" -gt 0 ]]; then + message="exit ${rc}" + fi + + if [[ -z "${message}" ]]; then + echo 'error: missing message' 1>&2 + return 1 + fi + + if [[ -z "${title}" ]]; then + title="${0}" + fi + + script='display notification "'"${message}"'"' + + if [[ -n "${title}" || -n "${subtitle}" || -n "${sound}" ]]; then + script+=' with' + fi + + if [[ -n "${title}" ]]; then + script+=' title "'"${title}"'"' + fi + + if [[ -n "${subtitle}" ]]; then + script+=' subtitle "'"${subtitle}"'"' + fi + + if [[ -n "${sound}" ]]; then + script+=' sound name "'"${sound}"'"' + fi + + osascript -e "${script}" + + return "${rc}" +} + +# vim: ft=bash