Right now, to the best of my knowledge, it is not possible to prevent the input/output switching between external devices and the onboard instruments using built-in features from the Operating System. I have added a simple trick (that you may already be aware of) to switch between input devices quickly and given an explanation as to why macOS behaves this way. I may not be the answer you are looking for but I wanted to share this information with you anyway, in the hope that it shows the problem from a different perspective or somebody is able to add to my thinking.
Input switching occurs when the new input/output device has microphone capabilities, which (in theory) gets the mic closer to the user thus produces greater audio and reducing background noise through less distance between the source and the mic - as the majority of headphones/earphones do with a built-in mic. This is how Apple views it and most customers don't want to put up with picking the right source every time they connect a pair of headphones.
Why might it be a positive?
Unlike in the Windows world, where a Default Device can be set in the Sound Settings, Macs (unlike PC's) have a built-in speaker and microphone in pretty much every single (recent) computer ever released. Microsoft does not restrict Windows to a select group of systems (the way macOS only works on an Apple device, with the exception of unsupported Hackintosh builds). That is a major factor in the lack of flexibility when it comes to different ways of settings the input devices because if Apple knows an interface exists, it becomes the default (and fallback) interface - there would be no reason to think otherwise.
Apple, known for its user experience, has decided that for the majority of its customers, the headphone goes into the socket, it detects whether it has microphone capabilities, switches inputs accordingly and is in that way able to deliver and send clearer audio between the mac and the headset (the headset presumably being closer to the user now than the onboard microphone).
A Common Workaround
The most common (and simplest) trick is to adjust the sound I/O interfaces themselves by switching to the desired device(s) through the menu bar.
- Find the Speaker icon in the menu bar (if not present, go to:
System Preferences >
Sound > Check/Enable:
Show volume in menu bar.
- Hold the Alt/Option key and click on the Speaker icon in the menu bar.
- You will now be able to switch between the devices as required. It may cost you a second or two of your time but it is by far a better alternative to messing with the system in other ways that may produce unknown side effects.
I hope I was able to shed some light on Apple's way of appealing to the majority and the reason why they believe auto switching is better than the manual way. I understand this is a rather long-winded explanation of a simple no but it was important to get the point across anyway.
Maybe you, like me, are able to forgive Apple for making an inflexible choice in their OS design that disregards a portion of the market's needs. I never found a reliable piece of software to prevent the switching so I learned to live with the quick menu bar trick and never let it bother me again. Maybe one day Apple adds a preference for preventing auto-switching of inputs.
Let me know if that was helpful in any way or if you have any other questions, I'd be more than happy to follow up with you on this.
Is there a way to switch inputs via AppleScript?
Absolutely, there is! Let me show you how.
tell application "System Preferences" to activate
tell application "System Preferences"
reveal anchor "input" of pane id "com.apple.preference.sound"
tell application "System Events" to tell process "System Preferences"
tell table 1 of scroll area 1 of tab group 1 of window 1
select (row 1 where value of text field 1 is "Line In") --> Or change to "Internal Microphone"
quit application "System Preferences"
You will need to allow the ScriptEditor to control your computer. Go to
System Preferences >
Security & Privacy >
Accessibility, then add the ScriptEditor/Compiled script to this list. This is an essential (security) step because your script is manipulating the GUI and in a way intercepting the window events. If you have no clue what any of that means, don't worry now, just paste the code into your ScriptEditor and run it - an error will tell you if anything was not done properly.
I'll leave you to your imagination but you could very easily make a toggle switch script (although the before mentioned menu bar trick pretty much does this already). One suggestion might be to find the
Terminal equivalent and simply run a
do shell script command through AppleScript. Such would involve looking for the
.plist file which contains the current state (input selection) of the Sound Preferences and manipulating it to your choice. This is highly OS-specific and has a tendency to require the user to log out for the changes to take effect. With that method, I think we are simply looking for trouble where we don't need it. As the age-old KISS principle states: "Keep it simple, stupid" - sometimes it's just better to do the simplest task that anyone without the technical knowledge (AppleScript/Terminal) can achieve.