Created
November 2, 2025 20:24
-
-
Save aeharding/387d91556ed9323a5b5522dddbdf6b28 to your computer and use it in GitHub Desktop.
Script to toggle Earpods (and save previous audio input/output) for hotkey. Pop!_OS 24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Toggle audio input/output between EarPods and your previous devices on Pop!_OS 24 | |
| set -euo pipefail | |
| STATE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/earpods_state" | |
| EARPODS_SINK="alsa_output.usb-Apple__Inc._EarPods_HQ9993P44X-00.analog-stereo" | |
| EARPODS_SOURCE="alsa_input.usb-Apple__Inc._EarPods_HQ9993P44X-00.mono-fallback" | |
| get_default_sink() { | |
| pactl info | grep "Default Sink" | awk '{print $3}' | |
| } | |
| get_default_source() { | |
| pactl info | grep "Default Source" | awk '{print $3}' | |
| } | |
| switch_to() { | |
| local sink="$1" | |
| local source="$2" | |
| echo "→ Switching output to $sink" | |
| pactl set-default-sink "$sink" | |
| for input in $(pactl list short sink-inputs | awk '{print $1}'); do | |
| pactl move-sink-input "$input" "$sink" | |
| done | |
| echo "→ Switching input to $source" | |
| pactl set-default-source "$source" | |
| for output in $(pactl list short source-outputs | awk '{print $1}'); do | |
| pactl move-source-output "$output" "$source" | |
| done | |
| } | |
| # Load state if it exists | |
| if [[ -f "$STATE_FILE" ]]; then | |
| source "$STATE_FILE" | |
| else | |
| PREV_SINK="" | |
| PREV_SOURCE="" | |
| fi | |
| CURRENT_SINK=$(get_default_sink) | |
| CURRENT_SOURCE=$(get_default_source) | |
| if [[ "$CURRENT_SINK" == "$EARPODS_SINK" && "$CURRENT_SOURCE" == "$EARPODS_SOURCE" ]]; then | |
| echo "EarPods are currently active — toggling back..." | |
| TARGET_SINK="${PREV_SINK:-$(pactl info | grep 'Default Sink' | awk '{print $3}')}" | |
| TARGET_SOURCE="${PREV_SOURCE:-$(pactl info | grep 'Default Source' | awk '{print $3}')}" | |
| switch_to "$TARGET_SINK" "$TARGET_SOURCE" | |
| rm -f "$STATE_FILE" | |
| else | |
| echo "Saving current devices and switching to EarPods..." | |
| mkdir -p "$(dirname "$STATE_FILE")" | |
| echo "PREV_SINK=$CURRENT_SINK" > "$STATE_FILE" | |
| echo "PREV_SOURCE=$CURRENT_SOURCE" >> "$STATE_FILE" | |
| switch_to "$EARPODS_SINK" "$EARPODS_SOURCE" | |
| fi | |
| echo "✅ Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment