Skip to content

Instantly share code, notes, and snippets.

@aronskaya
Created May 13, 2017 10:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aronskaya/efc58a4074f3f61eaa2d9e3b607d92dc to your computer and use it in GitHub Desktop.
Save aronskaya/efc58a4074f3f61eaa2d9e3b607d92dc to your computer and use it in GitHub Desktop.
[TISInputSource+Extensions] #tags: inputSource
extension TISInputSource {
var id: String {
let unsafeID = TISGetInputSourceProperty(self, kTISPropertyInputSourceID).assumingMemoryBound(to: CFString.self)
let name = Unmanaged<CFString>.fromOpaque(unsafeID).takeUnretainedValue()
return name as String
}
var type: String {
let unsafeID = TISGetInputSourceProperty(self, kTISPropertyInputSourceType).assumingMemoryBound(to: CFString.self)
let name = Unmanaged<CFString>.fromOpaque(unsafeID).takeUnretainedValue()
return name as String
}
var isEnabled: Bool {
let unsafeIsEnabled = TISGetInputSourceProperty(self, kTISPropertyInputSourceIsEnabled).assumingMemoryBound(to: CFBoolean.self)
let isEnabled = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(unsafeIsEnabled).takeUnretainedValue())
return isEnabled
}
var isSelected: Bool {
let unsafeIsSelected = TISGetInputSourceProperty(self, kTISPropertyInputSourceIsSelected).assumingMemoryBound(to: CFBoolean.self)
let isSelected = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(unsafeIsSelected).takeUnretainedValue())
return isSelected
}
var isEnableCapable: Bool {
let unsafeIsEnableCapable = TISGetInputSourceProperty(inputSource, kTISPropertyInputSourceIsEnableCapable).assumingMemoryBound(to: CFBoolean.self)
let isEnableCapable = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(unsafeIsEnableCapable).takeUnretainedValue())
return isEnableCapable
}
var isSelectCapable: Bool {
let unsafeIsSelectCapable = TISGetInputSourceProperty(inputSource, kTISPropertyInputSourceIsSelectCapable).assumingMemoryBound(to: CFBoolean.self)
let isSelectCapable = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(unsafeIsSelectCapable).takeUnretainedValue())
return isSelectCapable
}
func enable() {
if TISEnableInputSource(self) != noErr {
print("Input source enabling falied. Source: \(self)")
}
}
func disable() {
if TISDisableInputSource(self) != noErr {
print("Input source disabling falied. Source: \(self)")
}
}
func select() {
if TISSelectInputSource(self) != noErr {
print("input selection failed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment