Advertisement
muttmutt

usb-log-watcher.sh

Apr 6th, 2025
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.17 KB | Fixit | 0 0
  1. #!/bin/bash
  2.  
  3. PLUGGED="enumerated 0x05ac/110c/b817 (AirPods Max USB Audio / 1)"
  4. UNPLUGGED="(AirPods Max USB Audio): hardware connection lost"
  5. IDENTIFIER="AirPods Max"
  6.  
  7. PREVIOUS_STATE="disconnected"
  8.  
  9. log stream --predicate 'eventMessage contains "USB"' --info | while read -r line; do
  10.   if echo "$line" | grep -q "$IDENTIFIER"; then
  11.     if echo "$line" | grep -iq "$PLUGGED"; then
  12.       if [ "$PREVIOUS_STATE" != "connected" ]; then
  13.         #echo "[$(date)] AirPods Max connected – quitting SoundSource"
  14.         osascript -e 'tell application "SoundSource" to quit'
  15.         osascript -e 'display notification "AirPods Max connected – SoundSource quit" with title "USB Audio Event"'
  16.         PREVIOUS_STATE="connected"
  17.       fi
  18.     elif echo "$line" | grep -iq "$UNPLUGGED"; then
  19.       if [ "$PREVIOUS_STATE" != "disconnected" ]; then
  20.         #echo "[$(date)] AirPods Max disconnected – launching SoundSource"
  21.         osascript -e 'tell application "SoundSource" to activate'
  22.         osascript -e 'display notification "AirPods Max disconnected – SoundSource started" with title "USB Audio Event"'
  23.         PREVIOUS_STATE="disconnected"
  24.       fi
  25.     fi
  26.   fi
  27. done
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement