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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs