|
#!/bin/bash |
|
|
|
# Time Machine Launch and Quit Detector |
|
# This script monitors for Time Machine launches and quits using macOS logs |
|
|
|
echo "Starting Time Machine launch/quit detector..." |
|
echo "Press Ctrl+C to exit" |
|
|
|
# Track the last events to prevent duplicate notifications |
|
LAST_LAUNCH_DETECTED=0 |
|
LAST_QUIT_DETECTED=0 |
|
COOLDOWN_SECONDS=10 |
|
|
|
# Set app state tracker |
|
APP_RUNNING=false |
|
|
|
# We'll control Amethyst directly with AppleScript |
|
# No script paths needed |
|
|
|
# Function to handle launch events |
|
handle_launch() { |
|
CURRENT_TIME=$(date +%s) |
|
|
|
# Only trigger if we haven't recently detected a launch |
|
if (( CURRENT_TIME - LAST_LAUNCH_DETECTED > COOLDOWN_SECONDS )); then |
|
LAST_LAUNCH_DETECTED=$CURRENT_TIME |
|
APP_RUNNING=true |
|
|
|
# Get current date/time for logging |
|
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S") |
|
|
|
echo "===============================================" |
|
echo "Time Machine launch detected at $TIMESTAMP" |
|
echo "===============================================" |
|
|
|
# Quit Amethyst |
|
osascript -e 'tell application "Amethyst" to quit' |
|
|
|
echo "Amethyst has been quit" |
|
|
|
# Display a notification |
|
osascript -e 'display notification "Time Machine launched and Amethyst quit" with title "Time Machine Monitor"' |
|
|
|
echo "Launch handled, waiting for events..." |
|
fi |
|
} |
|
|
|
# Function to handle quit events |
|
handle_quit() { |
|
CURRENT_TIME=$(date +%s) |
|
|
|
# Only trigger if app was running and we haven't recently detected a quit |
|
if $APP_RUNNING && (( CURRENT_TIME - LAST_QUIT_DETECTED > COOLDOWN_SECONDS )); then |
|
LAST_QUIT_DETECTED=$CURRENT_TIME |
|
APP_RUNNING=false |
|
|
|
# Get current date/time for logging |
|
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S") |
|
|
|
echo "===============================================" |
|
echo "Time Machine quit detected at $TIMESTAMP" |
|
echo "===============================================" |
|
|
|
# Launch Amethyst |
|
osascript -e 'tell application "Amethyst" to activate' |
|
|
|
echo "Amethyst has been launched" |
|
|
|
# Display a notification |
|
osascript -e 'display notification "Time Machine quit and Amethyst launched" with title "Time Machine Monitor"' |
|
|
|
echo "Quit handled, waiting for events..." |
|
fi |
|
} |
|
|
|
# Use the full path to log to avoid shell builtin conflicts |
|
/usr/bin/log stream --predicate '(subsystem == "com.apple.runningboard") AND (eventMessage CONTAINS "com.apple.backup.launcher")' | while read -r line; do |
|
# Check for launch events |
|
if echo "$line" | grep -q "Launch request"; then |
|
handle_launch |
|
fi |
|
|
|
# Check for quit/termination events |
|
if echo "$line" | grep -E "process (exited|terminated)|assertion invalidated|no longer tracked|Process .* exited|Removing"; then |
|
handle_quit |
|
fi |
|
done |