Skip to content

Instantly share code, notes, and snippets.

@kiliankoe
Created January 17, 2021 00:47
Show Gist options
  • Save kiliankoe/c8a168dd0a1c0de7ff49d25a15525c43 to your computer and use it in GitHub Desktop.
Save kiliankoe/c8a168dd0a1c0de7ff49d25a15525c43 to your computer and use it in GitHub Desktop.
Never let macOS use my AirPods as an input device
import Foundation
import ShellOut
do {
try shellOut(to: "SwitchAudioSource -a")
} catch {
print("Couldn't find SwitchAudioSource.")
print("Please install switchaudio-osx via homebrew.")
exit(1)
}
let webcam = "HD Pro Webcam C920"
let macbook = "MacBook Pro Microphone"
func findAvailableInput() -> String {
let allInputDevices = try! shellOut(to: "SwitchAudioSource -t input -a").components(separatedBy: .newlines)
if allInputDevices.contains(where: { $0.contains(webcam) }) {
return webcam
}
return macbook
}
let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
let currentInputDevice = try! shellOut(to: "SwitchAudioSource -t input -c")
if currentInputDevice.contains("AirPods") {
let betterInput = findAvailableInput()
print("Input set to \(currentInputDevice) 😡 Changing to \(betterInput) instead.")
try! shellOut(to: "SwitchAudioSource -t input -s '\(betterInput)'")
}
}
RunLoop.main.run(until: .distantFuture)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment