MacOS: notify when the terminal command is finished

on Dragoș Străinu's blog

After reading A decade of dotfiles I wanted to create a similar bash function to boop.

And here it is:

# ~/.zshrc

# ...rest of zshrc

,notify () {
  local last_exit_status="$?"

  if [[ "$last_exit_status" == '0' ]]; then
    osascript -e "display notification \"Done\" with title \"Good\" sound name \"Fonk\""
  else
    osascript -e "display notification \"Exit code: $last_exit_status\" with title \"Bad\" sound name \"Ping\""
  fi
  $(exit "$last_exit_status")
}

It uses osascript to execute the AppleScripts that pushes the notification.
According to stackoverflow this is not the best way to pass bash variable to osascript, but I did not understand how to do it for my use case and as I use last_exit_status there should be no problem.

usage:

npm run test;,notify

I prefix the function name with a comma to faster search for it when doing ;,<Tab>.

You can test it with (exit 0);,notify and (exit 1);,notify.

notify good notify bad