Skip to content

Instantly share code, notes, and snippets.

@ultimatile
Last active March 10, 2024 13:47
Show Gist options
  • Save ultimatile/32885df57a545511dd09625adcea4bfe to your computer and use it in GitHub Desktop.
Save ultimatile/32885df57a545511dd09625adcea4bfe to your computer and use it in GitHub Desktop.
popup - A shell function to display a notification popup on macOS
#!/bin/zsh
# popup - A shell function to display a notification popup on macOS
#
# This function allows you to display a notification popup on macOS.
# It is useful for notifying users when a long-running process, such as a large build, has finished.
#
# Usage:
# popup [-M MESSAGE] [-F FAILED_MESSAGE] COMMAND
#
# Options:
# -M, --message MESSAGE Specify custom message for notification.
# The default is "Finished: $COMMAND".
# If MESSAGE contains two or more words, enclose it in double quotes.
# -F, --failed_message FAILED_MESSAGE Specify custom message for failed notification.
# The default is "Failed: $COMMAND".
# If FAILED_MESSAGE contains two or more words, enclose it in double quotes.
# -h, --help Display this help message."
#
# Examples:
# popup make -j8
# popup -M "Task completed" sleep 10
popup() { # Command name
help_message="$0 - execute a command and display a notification when it finishes.
usage: $0 [-M MESSAGE] [-F FAILED_MESSAGE] COMMAND
options:
-M, --message MESSAGE Specify custom message for notification.
The default is \"Finished: \$COMMAND\".
If MESSAGE contains two or more words, enclose it in double quotes.
-F, --failed_message FAILED_MESSAGE Specify custom message for failed notification.
The default is \"Failed: \$COMMAND\".
If FAILED_MESSAGE contains two or more words, enclose it in double quotes.
-h, --help Display this help message."
# Parse command line options
while [[ $# -gt 0 ]]; do
case $1 in
--help|-h)
# If --help or -h option is provided, display the help message
echo "$help_message"
return 0 # Exit with success status
;;
--message|-M)
# If --message or -M option is provided, set the custom message
succeeded_message="$2"
shift 2 # Move to the next option
;;
--failed_message|-F)
# If --failed_message or -F option is provided, set the custom message
failed_message="$2"
shift 2 # Move to the next option
;;
-*)
# Unknown option
echo "unknown option: $1"
echo "usage: $0 [-M message] [-F failed_message] command"
return 1 # Exit with error status
;;
*)
# Unknown option or argument, break out of the loop
break
;;
esac
done
# Define default messages if custom messages are not defined
! [[ -v succeeded_message ]] && succeeded_message="Finished:\n$@"
! [[ -v failed_message ]] && failed_message="Failed:\n$@"
# Execute the build command with its arguments
eval "$*"
if [ $? -eq 0 ]; then
osascript -e "tell app \"System Events\" to display dialog \"$succeeded_message\"" 1>/dev/null 2>/dev/null
else
osascript -e "tell app \"System Events\" to display dialog \"$failed_message\"" 1>/dev/null 2>/dev/null
fi
}
#The line below is required to run as a script like ./popup.sh [-M MESSAGE] [-F FAILED_MESSAGE] COMMAND
popup "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment