#!/bin/bash # 04-pause-music.sh — pause audio playback on connect, optionally resume on disconnect. # # Why you might want this: # You are SSHing in to share your screen. You forgot Spotify is playing # Taylor Swift through the speakers; the person on the other end is # listening to your music in real time. With this hook installed, audio # stops the moment the session starts and (optionally) resumes after it # ends. Privacy plus dignity. # # What it pauses: # - Music.app (formerly iTunes) # - Spotify (if installed and running) # - Apple Podcasts # Browser tabs and third-party media apps vary by macOS release and app # permission state, so this example intentionally targets named apps only. # # Setup: # Make the script executable, then add it from HearthGate's Hooks tab. # Full guide: https://codnamacs.com/hearthgate/help/features/hooks # # Toggle resume behaviour: # Set RESUME_ON_DISCONNECT=true below if you want music to start back up # when the session ends. Default is false because most users prefer # silence after the fact. # # Environment HearthGate provides: # HEARTHGATE_EVENT # # Test it: # open -a Music # or Spotify, start playback # HEARTHGATE_EVENT=on-connect ./04-pause-music.sh # HEARTHGATE_EVENT=after-disconnect ./04-pause-music.sh set -euo pipefail EVENT="${HEARTHGATE_EVENT:-${1:-unknown}}" RESUME_ON_DISCONNECT=false is_running() { osascript -e "tell application \"System Events\" to (name of processes) contains \"$1\"" \ | grep -qi true } pause_app() { local app="$1" if is_running "$app"; then osascript -e "tell application \"$app\" to pause" >/dev/null 2>&1 || true fi } play_app() { local app="$1" if is_running "$app"; then osascript -e "tell application \"$app\" to play" >/dev/null 2>&1 || true fi } case "$EVENT" in on-connect|after-connect) pause_app "Music" pause_app "Spotify" pause_app "Podcasts" # We intentionally do NOT send the media key on connect — if nothing # is playing, the key would unpause whatever was paused last. Pausing # each known app by name is safer. ;; on-disconnect|after-disconnect) if [ "$RESUME_ON_DISCONNECT" = true ]; then play_app "Music" play_app "Spotify" play_app "Podcasts" fi ;; *) echo "pause-music: unrecognised event '$EVENT'" >&2 ;; esac