Skip to content

Instantly share code, notes, and snippets.

@fl034
Last active April 6, 2026 00:53
Show Gist options
  • Select an option

  • Save fl034/dbf7e445d96a3979af734911aac6ebe0 to your computer and use it in GitHub Desktop.

Select an option

Save fl034/dbf7e445d96a3979af734911aac6ebe0 to your computer and use it in GitHub Desktop.
macOS Tahoe audio workaround script for external output devices. Uses switchaudio-osx to quickly switch to the internal output and back to the current external device, restoring audio when it gets stuck. Does nothing if the current output is already internal
#!/usr/bin/env bash
set -euo pipefail
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:${PATH:-}"
switch_audio_bin="${SWITCH_AUDIO_BIN:-}"
internal_prefix="${INTERNAL_AUDIO_PREFIX:-Mac}"
if [[ -z "$switch_audio_bin" ]]; then
for candidate in /opt/homebrew/bin/SwitchAudioSource /usr/local/bin/SwitchAudioSource SwitchAudioSource; do
if [[ -x "$candidate" ]] || command -v "$candidate" >/dev/null 2>&1; then
switch_audio_bin="$candidate"
break
fi
done
fi
if [[ -z "$switch_audio_bin" ]]; then
echo "error: SwitchAudioSource is not installed or not in PATH" >&2
echo "install with: brew install switchaudio-osx" >&2
echo "or set SWITCH_AUDIO_BIN to the full path, e.g. /opt/homebrew/bin/SwitchAudioSource" >&2
exit 1
fi
current_output_name="$($switch_audio_bin -c -t output -f human | head -n 1)"
if [[ -z "$current_output_name" ]]; then
echo "error: unable to determine current output device" >&2
exit 1
fi
if [[ "$current_output_name" == "$internal_prefix"* ]]; then
exit 0
fi
internal_output="$($switch_audio_bin -a -t output -f human | grep -E "^${internal_prefix}" | head -n 1 || true)"
if [[ -z "$internal_output" ]]; then
echo "error: unable to find an internal output device" >&2
echo "set INTERNAL_AUDIO_PREFIX if your built-in output does not start with 'Mac'" >&2
exit 1
fi
"$switch_audio_bin" -t output -s "$internal_output" >/dev/null
"$switch_audio_bin" -t output -s "$current_output_name" >/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment