Question

I have a script, and I want it to behave differently if headphones are connected. How can I get this information?

I tried using SwitchAudioSource, but its output is the same, whether or not the headphones are connected:

~/tmp
❯ SwitchAudioSource -c
Built-in Output

~/tmp
❯ SwitchAudioSource -a
Built-in Microphone (input)
Background Music (input)
Background Music (UI Sounds) (input)
Built-in Output (output)
Background Music (output)
Background Music (UI Sounds) (output)
Was it helpful?

Solution

I found an answer from here:

if system_profiler SPAudioDataType | grep --quiet Headphones; then
  echo plugged in
else
  echo not plugged in
fi
Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top