3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[macOS][Cocoa][Swift]IMEを変更させる

Last updated at Posted at 2020-12-15

IMEの変更

ソース

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)
    }
    
    // 確認用
    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]
    }
}
// IMEを変更
InputSource.change(id: "com.google.inputmethod.Japanese.base")

確認用の出力結果

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"]]
--------------------
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?