Skip to content

Instantly share code, notes, and snippets.

@ajorpheus
Last active March 26, 2025 12:48
Show Gist options
  • Save ajorpheus/e72f99777968f387971f05007b0eae85 to your computer and use it in GitHub Desktop.
Save ajorpheus/e72f99777968f387971f05007b0eae85 to your computer and use it in GitHub Desktop.
Monitor launch of Time Machine App and quit or launch Amethyst accordingly
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.timemachine-amethyst-monitor</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/Users/az/Dropbx/scripts/tm-amethyst-manager.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/timemachine-amethyst-monitor.err</string>
<key>StandardOutPath</key>
<string>/tmp/timemachine-amethyst-monitor.out</string>
</dict>
</plist>

Description

This script and accompanying LaunchAgent automatically monitor the launch of Time Machine application and quit Amethyst automatically and also re-launches Amethyst when Time Machine quits.

Notes

Depending on where you save this script update it's path in the LaunchAgent plist under ProgramArguments.

Troubleshoot issues with LaunchAgent

Monitor this file for errors : /tmp/timemachine-amethyst-monitor.out

Final Result

2025-03-26_12-29-49

#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment