Terminal: Trigger macOS Notification from Shell
Notifications are very useful within macOS. The terminal, shell, does not provide a solution for triggering notitications. Being notified by a notification at the end of a long running command/script could save some time.
Without installing any other applications, the best way I found is using some AppleScript as a shell function.
Just add the following function to your .bashrc
or .zshrc
:
function _sys_notify() {
local notification_command="display notification \"$2\" with title \"$1\" sound name \"Sonumi\""
osascript -e "$notification_command"
}
alias sys-notify="_sys_notify $1 $2"
Triggering a notification:
$ sys-notify "My Nice Title" "With a longer message"
Bonus: A nice Sonumi sound is played.
Here is the result:
Reference: The Apple documentation