import Carbon
import Foundation
func prop(_ s:TISInputSource,_ k:CFString)->String { guard let p=TISGetInputSourceProperty(s,k) else{return ""}; return Unmanaged<CFString>.fromOpaque(p).takeUnretainedValue() as String }
let current=TISCopyCurrentKeyboardInputSource().takeRetainedValue()
print("current="+prop(current,kTISPropertyInputSourceID))
if CommandLine.arguments.count>1 {
 let wanted=CommandLine.arguments[1]
 let list=TISCreateInputSourceList(nil,true).takeRetainedValue() as! [TISInputSource]
 for s in list where prop(s,kTISPropertyInputSourceID)==wanted { print("enable=\(TISEnableInputSource(s)) select=\(TISSelectInputSource(s))") }
 print("selected="+prop(TISCopyCurrentKeyboardInputSource().takeRetainedValue(),kTISPropertyInputSourceID))
}
