Skip to content

Instantly share code, notes, and snippets.

@marcastel
Last active April 9, 2017 14:05
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 marcastel/16d029b70383de0eae37b0407d893d58 to your computer and use it in GitHub Desktop.
Save marcastel/16d029b70383de0eae37b0407d893d58 to your computer and use it in GitHub Desktop.
df_notifier -- macOS user notifications (à la Growl) from the command line
#!/bin/ksh
# @{ <script> df_uxnotifier
[[ -z $DF_NOTIFIERAPP ]] && DF_NOTIFIERAPP=/Applications/terminal-notifier.app
[[ -z $DF_NOTIFIERBIN ]] && DF_NOTIFIERBIN=/Contents/MacOS/terminal-notifier
# @{ USAGE
typeset -S usage=$'[-1]
[+NAME?df_notifier -- enact macOS user notifications]
[+DESCRIPTION?\adf_notifier\a is a Korn shell script wrapper around Julien Blanchard\'s \aTerminal Notifier\a application that
allows to send user notifications from the command line or shebang\'ed shell \vexec(1)\v\'s. MacOS user notifications,
which are available on macOS 10.8 and higher.]
[+?Usage of this utility is very straightforward, see the \aEXAMPLES\a section below. At a minimum, you must specify either
the \b--message\b , the \b--remove\b, or the \b--list\b option.]
[a:actions?Use \bVALUES\b as actions for the notification, where \bVALUES\b is a comma separated list of shell words or
strings. Multiple values will be displayed as a dropdown; you can customize this dropdown label with the
\b--dropdownLabel\b option. Does not work when \b--reply\b is used. [macOS 0.9+]].]:[VALUES]
[A:activate?Activate the application specified by \bID\b when the user clicks the notification. You can find the bundle
identifier (\bCFBundleIdentifier\b) of an application in its \bInfo.plist\b file inside the application bundle. For
instance \bcom.apple.Terminal\b to activate Terminal.app, or \bcom.apple.Safari\b to activate Safari.app.]:[ID]
[B:sound?Play the \bNAME\b sound when the notification appears. Sound names are listed in Sound Preferences. Use the special
\bNAME\b default for the default notification sound.]:[NAME]
[C:close-label?Use the \bVALUE\b label for the notifications \bClose\b button. [macOS 10.9+]].]
[e:execute?Run the shell command \bCOMMAND\b when the user clicks the notification.]:[COMMAND]
[g:group?Specifies the notifications group. For any group, only one notification will ever be shown, replacing previously
posted notifications. A notification can be explicitly removed with the \b--remove\b option. Example group IDs:]:[ID]
[+?]{
[+?- The senders name (to scope the notifications by tool).]
[+?- The senders process ID (to scope the notifications by a unique process).]
[+?- The current working directory (to scope notifications by project).]}
[i:app-icon?Specify an image \bPATH\b to display instead of the application icon [macOS 10.9+]].]:[PATH]
[I:content-image?Specify an image \bPATH\b to attach inside of the notification [macOS 10.9+]].]:[PATH]
[j:json?Output the event as JSON.]
[l:list?Lists details about the specified group \bID\b. Use the special \bID\b \vALL\v to list details about all currently
active messages. The output of this command is tab-separated, which makes it easy to parse.]:[ID:=ALL]
[L:dropdown-label?Use the \bVALUE\b label for the notifications dropdown menu actions (in which case it is assumed that
\b--actions\b contains more than one action). Does not work when \b--reply\b is used. [10.9+]]]:[VALUE]
[m:message?The message body of the notification. If you pipe data into terminal-notifier, you can omit this option, and the
piped data will become the message body instead.]:[VALUE]
[N:notifier-app?Use this option or set the environment variable \aDF_NOTIFIERAPP\a to the path of the \aTerminal Notifier\a
application (e.g. \b/Applications/MyRenamed.app\b). Otherwise the default name and location will be used.]:
[PATH:='$DF_NOTIFIERAPP$']
[o:open?Open \bURL\b when the user clicks the notification. This can be a web or file URL, or any custom URL scheme.]:[URL]
[r:remove?Remove a previous notification from the \bID\b group, if one exists. Use the special \bID\b \vALL\v to remove all
messages.]:[ID:=ALL]
[R:reply?Display the notification as a reply type alert [macOS 10.9+]].]
[s:subtitle?The subtitle of the notification.]:[VALUE]
[S:sender?Fakes the sender application of the notification. This uses the specified applications icon, and will launch it
when the notification is clicked. Because this option fakes the notification sender, it cannot be used with options like
\b--execute\b, \b--open\b, and \b--activate\b. For information on the \bID\b, see the \b--activate\b option.]:[ID]
[t:title?The title of the notification. This defaults to Terminal.]:[VALUE]
[T:timeout?Automatically close the alert notification after \bNUMBER\b seconds.]:[NUMBER]
[+EXAMPLES]
[+?Display piped data with a sound:][+\
echo "Piped Message Data!" | df_notifier --sound default]
[+?Multiple actions and custom dropdown list:][+\
df_notifier \
--message "Deploy now on UAT ??" \
--actions Now,"Later today","Tomorrow" \
--dropdownLabel "When ??"]
[+?Yes or No ?:][+\
df_notifier \
--title ProjectX
--subtitle "new tag detected" \
--message "Deploy now on UAT ??" \
--close-label No \
--actions Yes \
--app-icon http:://vjeantet.fr/images/logo.png]
[+?Yes or No ? (shorthand notation):][+\
df_notifier -t ProjectX -s "new tag detected" -m "Deploy now on UAT ??" -C No -a Yes -i http::...]
[+?Open an URL when the notification is clicked:][+\
df_notifier \
--title="💰" \
--message="Check your Apple stock!" \
--open="http:://finance.yahoo.com/q??s=AAPL"]
[+?Open an app when the notification is clicked:][+\
df_notifier \
--group "address-book-sync" \
--title "Address Book Sync" \
--subtitle "Finished" \
--message "Imported 42 contacts." \
--activate "com.apple.AddressBook"]
[+DEPENDENCIES?Julien Blanchard\'s \aTerminal Notifier\a application. You can
download the binary distribution directly:][+\
https:://github.com/julienXX/terminal-notifier/releases]
[+?See Julian\'s GitHub project page for alternate ways of downloading or building this utility:][+\
https:://github.com/alloy/terminal-notifier]
[+COMPATIBILITY?This script complies to conventional POSIX, UNIX, and Linux conventions through the use of double dashes for
long options. If this is not a desired feature and you wish to use a single dash for long options, then you can change the
following line in this script:][+\
typeset -S usage=$\'[-1]]]
[+?to:][\+
typeset -S usage=$\'[-1p1]]]
[+?Note however that we do not currently support this modification. It should work out of the box as it doesn\'t change
the logic of the script. However it is not tested. And typically this help text will have to we reworked to comply to the
new syntax]'
# @}
# @{ CLI
typeset actions="" appopts="" optname
while getopts -a ${0##*/} "$usage" option; do case $option in
[ABCegiIlLmorRsStT])
case $option in
A) optname=activate ;; B) optname=sound ;; C) optname=closeLabel ;;
e) optname=execute ;; g) optname=group ;; i) optname=appIcon ;;
I) optname=contentImage ;; l) optname=list ;; L) optname=dropdownLabel;;
m) optname=message ;; o) optname=open ;; r) optname=remove ;;
R) optname=reply ;; s) optname=subtitle ;; S) optname=sender ;;
t) optname=title ;; T) optname=timeout ;;
esac; appopts+=" -$optname \"$OPTARG\"" ;;
a) [[ -z $actions ]] && actions="$OPTARG" || actions+=",$OPTARG" ;;
j) appopts+=" -json" ;;
N) DF_NOTIFIERAPP="$OPTARG" ;;
esac; done; (( OPTIND > 1 )) && shift $(( OPTIND - 1 ))
# @}
# @{ MAIN
[[ -n $actions ]] && appopts+=" -actions \"$actions\""
typeset app=$DF_NOTIFIERAPP bin="${DF_NOTIFIERAPP%/}/${DF_NOTIFIERBIN#/}"
[[ -d $app ]] || { print -u2 "${0##*/}: unable to locate the Terminal Notifier application."; exit 1; }
[[ -x $bin ]] || { print -u2 "${0##*/}: terminal notifier binary not found or not accessible."; exit 1; }
eval $bin ${appopts## }
# @}
# @}
#! @revision Sun Apr 09, 2017 15:45:57
#! @author JM Marcastel
#! @brief send macOS user notifications from th command line
# vim: nu et tw=130 ts=8 sts=4 sw=4 ff=unix fo-=l fo+=tcroq2 fdm=marker fmr=@{,@}
revision author title
Sun Apr 09, 2017 15:56:55
JM Marcastel
df_notifier -- user commands

Name

df_notifier -- enact macOS user notifications

SYNOPSIS

df_notifier [options]

DESCRIPTION

df_notifier is a Korn shell script wrapper around Julien Blanchard's Terminal Notifier application that allows to send user notifications from the command line or shebang'ed shell exec(1)'s. MacOS user notifications, which are available on macOS 10.8 and higher.

Usage of this utility is very straightforward, see the EXAMPLES section below. At a minimum, you must specify either the --message , the --remove , or the --list option.

OPTIONS

-a, --actions=_VALUES_ : Use VALUES as actions for the notification, where VALUES is a comma separated list of shell words or strings. Multiple values will be displayed as a dropdown; you can customize this dropdown label with the --dropdownLabel option. Does not work when --reply is used. [macOS 0.9+].

-A, --activate=_ID_ : Activate the application specified by ID when the user clicks the notification. You can find the bundle identifier (CFBundleIdentifier) of an application in its Info.plist file inside the application bundle. For instance com.apple.Terminal to activate Terminal.app, or com.apple.Safari to activate Safari.app.

-B, --sound=_NAME_ : Play the NAME sound when the notification appears. Sound names are listed in Sound Preferences. Use the special NAME default for the default notification sound.

-C, --close-label : Use the VALUE label for the notifications Close button. [macOS 10.9+].

-e, --execute=_COMMAND_ : Run the shell command COMMAND when the user clicks the notification.

-g, --group=_ID_ : Specifies the notifications group. For any group, only one notification will ever be shown, replacing previously posted notifications. A notification can be explicitly removed with the --remove option. Example group IDs:

-   The senders name (to scope the notifications by tool).
-   The senders process ID (to scope the notifications by a unique process).
-   The current working directory (to scope notifications by project).

-i, --app-icon=_PATH_ : Specify an image PATH to display instead of the application icon [macOS 10.9+].

-I, --content-image=_PATH_ : Specify an image PATH to attach inside of the notification [macOS 10.9+].

-j, --json : Output the event as JSON.

-l, --list=_ID_ : Lists details about the specified group ID. Use the special ID ALL to list details about all currently active messages. The output of this command is tab-separated, which makes it easy to parse. The default value is ALL.

-L, --dropdown-label=_VALUE_ : Use the VALUE label for the notifications dropdown menu actions (in which case it is assumed that --actions contains more than one action). Does not work when --reply is used. [10.9+]

-m, --message=_VALUE_ : The message body of the notification. If you pipe data into terminal-notifier, you can omit this option, and the piped data will become the message body instead. -N, --notifier-app=_PATH_ : Use this option or set the environment variable DF_NOTIFIERAPP to the path of the Terminal Notifier application (e.g. /Applications/MyRenamed.app). Otherwise the default name and location will be used. The default value is /Applications/terminal-notifier.app.

-o, --open=_URL_ : Open URL when the user clicks the notification. This can be a web or file URL, or any custom URL scheme.

-r, --remove=_ID_ : Remove a previous notification from the ID group, if one exists. Use the special ID ALL to remove all messages. The default value is ALL.

-R, --reply : Display the notification as a reply type alert [macOS 10.9+].

-s, --subtitle=_VALUE_ : The subtitle of the notification.

-S, --sender=_ID_ : Fakes the sender application of the notification. This uses the specified applications icon, and will launch it when the notification is clicked. Because this option fakes the notification sender, it cannot be used with options like --execute, --open, and --activate. For information on the ID, see the --activate option.

-t, --title=_VALUE_ : The title of the notification. This defaults to Terminal.

-T, --timeout=_NUMBER_ : Automatically close the alert notification after NUMBER seconds.

EXAMPLES

Display piped data with a sound:

echo "Piped Message Data!" | df_notifier --sound default

Multiple actions and custom dropdown list:

df_notifier \
    --message "Deploy now on UAT ?" \
    --actions Now,"Later today","Tomorrow" \
    --dropdownLabel "When ?"

Yes or No ?:

df_notifier \
    --title ProjectX \
    --subtitle "new tag detected" \
    --message "Deploy now on UAT ?" \
    --close-label No \
    --actions Yes \
    --app-icon http://vjeantet.fr/images/logo.png \

Yes or No ? (shorthand notation):

df_notifier -t ProjectX -s "new tag detected" -m "Deploy now on UAT ?" -C No -a Yes -i http:...

Open an URL when the notification is clicked:

df_notifier \
    --title="💰" \
    --message="Check your Apple stock!" \
    --open="http://finance.yahoo.com/q?s=AAPL" \

Open an app when the notification is clicked:

df_notifier \
    --group "address-book-sync" \
    --title "Address Book Sync" \
    --subtitle "Finished" \
    --message "Imported 42 contacts." \
    --activate "com.apple.AddressBook"

DEPENDENCIES

Julien Blanchard's Terminal Notifier application. You can download the binary distribution directly:
https://github.com/julienXX/terminal-notifier/releases

See Julian's GitHub project page for alternate ways of downloading or building this utility:
https://github.com/alloy/terminal-notifier

COMPATIBILITY

This script complies to conventional POSIX, UNIX, and Linux conventions through the use of double dashes for long options. If this is not a desired feature and you wish to use a single dash for long options, then you can change the following line in this script:

typeset -S usage=$'[-1]

to:

typeset -S usage=$'[-1p1]

Note however that we do not currently support this modification. It should work out of the box as it doesn't change the logic of the script. However it is not tested. And typically this help text will have to we reworked to comply to the new syntax

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