Skip to content

Instantly share code, notes, and snippets.

@ChrisColeTech
Created August 17, 2025 01:00
Show Gist options
  • Select an option

  • Save ChrisColeTech/c57539fb0e5f8b68db9c15653490c318 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisColeTech/c57539fb0e5f8b68db9c15653490c318 to your computer and use it in GitHub Desktop.
Windows Toast Notifications - Bash Installation Script for macOS
#!/usr/bin/env bash
# macOS Toast Notification Installer
# This script installs a toast() function for macOS using native AppleScript
# Usage: bash macos_toast_install.sh
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_header() {
echo -e "${BLUE}[HEADER]${NC} $1"
}
# Configuration
RC_FILES=(~/.bashrc ~/.zshrc ~/.bash_profile ~/.profile)
SNIPPET_START="# -- macos toast helper start --"
SNIPPET_END="# -- macos toast helper end --"
echo "=== macOS Toast Notification Installer ==="
echo
# Check if we're actually on macOS
if [[ "$(uname)" != "Darwin" ]]; then
print_error "This installer is for macOS only"
print_error "Current system: $(uname)"
print_error "For other systems, use:"
print_error " Windows: README-Windows.md"
print_error " WSL: README-WSL.md"
exit 1
fi
print_status "Detected macOS system: $(sw_vers -productName) $(sw_vers -productVersion)"
# Test AppleScript notification capability
print_status "Testing macOS notification system..."
if osascript -e 'display notification "Testing macOS notifications" with title "Toast Installer"' 2>/dev/null; then
print_status "✓ Native AppleScript notifications working"
else
print_warning "AppleScript notifications may not be working properly"
print_warning "Continuing with installation..."
fi
# Check for optional enhancers
print_status "Checking for optional notification enhancers..."
# Check for terminal-notifier
if command -v terminal-notifier >/dev/null 2>&1; then
print_status "✓ Found terminal-notifier - will use for enhanced features"
USE_TERMINAL_NOTIFIER=true
else
print_status "terminal-notifier not found - using native AppleScript only"
print_status "To install terminal-notifier later: brew install terminal-notifier"
USE_TERMINAL_NOTIFIER=false
fi
# Check for alerter
if command -v alerter >/dev/null 2>&1; then
print_status "✓ Found alerter - available for advanced features"
USE_ALERTER=true
else
USE_ALERTER=false
fi
# Create the toast function based on available tools
if [[ "$USE_TERMINAL_NOTIFIER" == true ]]; then
print_status "Creating enhanced toast function with terminal-notifier"
SNIPPET="$SNIPPET_START
toast() {
local title=\"\${1:-Notification}\"
shift
local body=\"\${*:-}\"
# Use terminal-notifier for enhanced features if available
if command -v terminal-notifier >/dev/null 2>&1; then
if [[ -n \"\$body\" ]]; then
terminal-notifier -title \"\$title\" -message \"\$body\" -sound default
else
terminal-notifier -title \"Notification\" -message \"\$title\" -sound default
fi
else
# Fallback to native AppleScript
if [[ -n \"\$body\" ]]; then
osascript -e \"display notification \\\"\$body\\\" with title \\\"\$title\\\"\"
else
osascript -e \"display notification \\\"\$title\\\" with title \\\"Notification\\\"\"
fi
fi
}
$SNIPPET_END"
else
print_status "Creating basic toast function with native AppleScript"
SNIPPET="$SNIPPET_START
toast() {
local title=\"\${1:-Notification}\"
shift
local body=\"\${*:-}\"
# Use native macOS AppleScript notifications
if [[ -n \"\$body\" ]]; then
osascript -e \"display notification \\\"\$body\\\" with title \\\"\$title\\\"\"
else
osascript -e \"display notification \\\"\$title\\\" with title \\\"Notification\\\"\"
fi
}
$SNIPPET_END"
fi
print_status "Installing macOS toast helper..."
# Install to shell configuration files
installed_count=0
for rc in "${RC_FILES[@]}"; do
rc_expanded="$(eval echo "${rc}")"
# Skip if file doesn't exist and it's not a primary shell config
if [[ ! -f "$rc_expanded" && ! "$rc_expanded" =~ (\.zshrc|\.bashrc|\.bash_profile)$ ]]; then
continue
fi
# Create file if it doesn't exist (for primary configs)
if [[ ! -f "$rc_expanded" ]]; then
print_status "Creating $rc_expanded"
touch "$rc_expanded"
fi
# Check if snippet already present
if grep -Fq "$SNIPPET_START" "$rc_expanded" 2>/dev/null; then
print_warning "Toast helper already present in $rc_expanded"
else
print_status "Adding toast helper to $rc_expanded"
echo "" >> "$rc_expanded"
printf "%s\n" "$SNIPPET" >> "$rc_expanded"
installed_count=$((installed_count + 1))
fi
done
if [[ $installed_count -eq 0 ]]; then
print_warning "Toast helper was already installed or no suitable shell config files found"
else
print_status "Toast helper installed to $installed_count shell configuration file(s)"
fi
echo
print_header "Installation complete!"
echo
# Provide usage instructions
print_status "To enable the toast function in your current session:"
echo " source ~/.zshrc # if you use zsh"
echo " source ~/.bashrc # if you use bash"
echo " source ~/.bash_profile # alternative for bash"
echo
echo "Or open a new terminal window."
echo
print_status "Usage examples:"
echo ' toast "Hello" "This is a test notification"'
echo ' toast "Build Complete"'
echo ' toast "Error" "Something went wrong"'
echo
# Test installation
print_status "Testing installation..."
# Try to test with the current shell
if [[ -n "${BASH_VERSION:-}" ]]; then
shell_config="~/.bashrc"
if [[ -f ~/.bash_profile ]]; then
shell_config="~/.bash_profile"
fi
elif [[ -n "${ZSH_VERSION:-}" ]]; then
shell_config="~/.zshrc"
else
shell_config="~/.profile"
fi
# Source the config and test
eval_expanded=$(eval echo "$shell_config")
if [[ -f "$eval_expanded" ]]; then
if bash -c "source $eval_expanded && toast 'Installation Complete' 'macOS toast notifications are ready'" 2>/dev/null; then
print_status "✓ Test notification sent successfully!"
else
print_warning "Test notification failed - you may need to restart your terminal"
fi
fi
# Additional information
echo
print_header "Additional Information:"
echo
if [[ "$USE_TERMINAL_NOTIFIER" == false ]]; then
print_status "Optional: Install terminal-notifier for enhanced features:"
echo " brew install terminal-notifier"
echo " # Provides sound, icons, and click actions"
echo
fi
if [[ "$USE_ALERTER" == false ]]; then
print_status "Optional: Install alerter for advanced notifications:"
echo " brew install alerter"
echo " # Provides buttons, inputs, and more customization"
echo
fi
print_status "Features available:"
echo " ✓ Native macOS notifications (always available)"
if [[ "$USE_TERMINAL_NOTIFIER" == true ]]; then
echo " ✓ Enhanced notifications with terminal-notifier"
fi
if [[ "$USE_ALERTER" == true ]]; then
echo " ✓ Advanced notifications with alerter"
fi
echo
print_status "For Claude Code integration, see: README-macOS.md"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment