MacOS: notify when the terminal command is finished

strdr4605

Dragoș Străinu

Posted on April 21, 2023

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

💖 💪 🙅 🚩
strdr4605
Dragoș Străinu

Posted on April 21, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related