Skip to content

Instantly share code, notes, and snippets.

@lucaswilric
Last active July 21, 2021 01:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucaswilric/b7c68bb5ae502990cccf3842c2a2c2bd to your computer and use it in GitHub Desktop.
Save lucaswilric/b7c68bb5ae502990cccf3842c2a2c2bd to your computer and use it in GitHub Desktop.
Bash wrapper script to create an OSX notification when my command completes
#!/usr/bin/env bash
## Create an OSX notification when my command completes.
#
# Usage: notify-done.sh <command>
#
# For convenience, use a Bash alias:
#
# alias n="notify-done.sh "
#
# (The trailing space tells the shell to expand any following aliases, so you
# can use your existing aliases with `n`).
# First, capture the input and give it a descriptive name.
#
cmd="$@"
# Then, execute it, passing in the current environment.
#
env bash -c "$cmd"
# Capture the result so that we can exit transparently (i.e. so that this script
# doesn't "swallow" exit codes that might be important) when the time comes.
#
exit_code=$?
# Compose a notification message.
#
msg="$exit_code: $1 has completed."
# Decide on a notification title.
#
if [[ "$exit_code" == "0" ]]
then
result=Success
else
result=Failure
fi
# Display the notification, using the message and title we put together above.
#
# You can see the list of available sounds by opening System Preferences >
# Sounds > Sound Effects.
#
osascript -e "display notification \"$msg\" with title \"$result\" sound name \"Glass\""
# Exit transparently, because using this notification wrapper shouldn't change
# how the wrapped command be haves -- that would be rude.
#
exit $exit_code
@bitwombat
Copy link

For anybody on Linux, you might have notify-send, in which case replace the 'osascript' line with this:

/usr/bin/notify-send -u critical "$result" "$msg"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment