DEV Community

Cover image for MacOS: notify when the terminal command is finished
Dragoș Străinu
Dragoș Străinu

Posted on • Originally published at strdr4605.com on

MacOS: notify when the terminal command is finished

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")
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)