[macOS] [Cocoa] [Swift] Change IME
IME changes
Source
import Cocoa
import InputMethodKit
class InputSource {
fileprivate static var inputSources: [TISInputSource] {
let inputSourceNSArray = TISCreateInputSourceList(nil, false).takeRetainedValue() as NSArray
return inputSourceNSArray as![TISInputSource]
}
fileprivate static var selectCapableInputSources: [TISInputSource] {
return inputSources.filter({ $0.isSelectCapable })
}
static func change(id: String) {
guard let inputSource = selectCapableInputSources.filter({ $0.id == id }).first else { return }
TISSelectInputSource(inputSource)
}
//For confirmation
static func print() {
for source in inputSources {
Swift.print("id:[\(source.id)]")
Swift.print("localizedName:[\(source.localizedName)]")
Swift.print("isSelectCapable:[\(source.isSelectCapable)]")
Swift.print("isSelected:[\(source.isSelected)]")
Swift.print("sourceLanguages:[\(source.sourceLanguages)]")
Swift.print("--------------------")
}
}
}
extension TISInputSource {
func getProperty(_ key: CFString) -> AnyObject? {
guard let cfType = TISGetInputSourceProperty(self, key) else { return nil }
return Unmanaged<AnyObject>.fromOpaque(cfType).takeUnretainedValue()
}
var id: String {
return getProperty(kTISPropertyInputSourceID) as! String
}
var localizedName: String {
return getProperty(kTISPropertyLocalizedName) as! String
}
var isSelectCapable: Bool {
return getProperty(kTISPropertyInputSourceIsSelectCapable) as! Bool
}
var isSelected: Bool {
return getProperty(kTISPropertyInputSourceIsSelected) as! Bool
}
var sourceLanguages: [String] {
return getProperty(kTISPropertyInputSourceLanguages) as![String]
}
}
//Change IME
InputSource.change(id: "com.google.inputmethod.Japanese.base")
Output result for confirmation
id:[com.apple.inputmethod.Kotoeri.Japanese]
localizedName:[Hiragana]
isSelectCapable:[true]
isSelected:[false]
sourceLanguages:[["ja"]]
--------------------
id:[com.apple.inputmethod.Kotoeri.Roman]
localizedName:[Romaji]
isSelectCapable:[true]
isSelected:[false]
sourceLanguages:[["en"]]
--------------------
id:[com.apple.inputmethod.Kotoeri]
localizedName:[Japanese]
isSelectCapable:[false]
isSelected:[false]
sourceLanguages:[["ja", "en"]]
--------------------
id:[com.apple.inputmethod.Kotoeri.Japanese.Katakana]
localizedName:[Katakana]
isSelectCapable:[true]
isSelected:[false]
sourceLanguages:[["ja"]]
--------------------
id:[com.apple.CharacterPaletteIM]
localizedName:[Emoji & Symbols]
isSelectCapable:[true]
isSelected:[false]
sourceLanguages:[["en"]]
--------------------
id:[com.apple.50onPaletteIM]
localizedName:[Japanese Kana Palette]
isSelectCapable:[true]
isSelected:[false]
sourceLanguages:[["ja"]]
--------------------
id:[com.apple.inputmethod.Kotoeri.Japanese.HalfWidthKana]
localizedName:[Half-width Katakana]
isSelectCapable:[true]
isSelected:[false]
sourceLanguages:[["ja"]]
--------------------
id:[com.google.inputmethod.Japanese.Katakana]
localizedName:[Katakana (Google)]
isSelectCapable:[true]
isSelected:[false]
sourceLanguages:[["ja"]]
--------------------
id:[com.google.inputmethod.Japanese.HalfWidthKana]
localizedName:[Half-width Katakana (Google)]
isSelectCapable:[true]
isSelected:[false]
sourceLanguages:[["ja"]]
--------------------
id:[com.google.inputmethod.Japanese.Roman]
localizedName:[Alphanumeric (Google)]
isSelectCapable:[true]
isSelected:[true]
sourceLanguages:[["en"]]
--------------------
id:[com.google.inputmethod.Japanese.FullWidthRoman]
localizedName:[Full-width Alphanumeric (Google)]
isSelectCapable:[true]
isSelected:[false]
sourceLanguages:[["ja"]]
--------------------
id:[com.google.inputmethod.Japanese.base]
localizedName:[Hiragana (Google)]
isSelectCapable:[true]
isSelected:[false]
sourceLanguages:[["ja"]]
--------------------
id:[com.google.inputmethod.Japanese]
localizedName:[Google Japanese Input]
isSelectCapable:[false]
isSelected:[false]
sourceLanguages:[["ja", "en"]]
--------------------