Cocoaからのライブ変換(LiveConversion)の変更と監視

今の所、専用のAPIは見つかっていない。設定ファイルやDistributedNotificationの監視で見つかった事を書く。

  • 変更の監視
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
   [[NSDistributedNotificationCenter defaultCenter]
           addObserver:self
              selector:@selector(jimPreferencesDidChangeNotification:)
                  name:@"com.apple.inputmethod.JIM.PreferencesDidChangeNotification"
               object:nil
    suspensionBehavior:0];
}

- (void) jimPreferencesDidChangeNotification:(NSNotification*)note
{
    NSLog(@"jimPreferencesDidChangeNotification => %@",
          [note.userInfo valueForKey:@"JIMPrefLiveConversionKey"]);
}
  • 現在の設定の確認
NSUserDefaults* theUD = [[NSUserDefaults alloc] initWithSuiteName:@"com.apple.inputmethod.Kotoeri"];

NSLog(@"[theUD valueForKey:JIMPrefLiveConversionKey] => %@",
        [theUD valueForKey:@"JIMPrefLiveConversionKey"]);

Warning

ImputMethodのprefarenceを参照しているだけなので、sandbox環境では正常に動作しない。

  • ライブ変換を有効/無効にする

有効

[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.inputmethod.JIM.PreferencesDidChangeNotification"
                                                                  object:@"com.apple.JIMSession"
                                                                userInfo:/*@{@"JIMPrefLiveConversionKey" : @(1)}*/ // <--コメントアウトを外して使用すること
                                                                 options:0];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.inputmethod.JIM.PreferencesDidChangeNotification"
                                                                  object:@"com.apple.JIMPreferences"
                                                                userInfo:/*@{@"JIMPrefLiveConversionKey" : @(1)}*/ // <--コメントアウトを外して使用すること
                                                                 options:0];

無効

[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.inputmethod.JIM.PreferencesDidChangeNotification"
                                                               object:@"com.apple.JIMSession"
                                                             userInfo:/*@{@"JIMPrefLiveConversionKey" : @(0)}*/ // <--コメントアウトを外して使用すること
                                                              options:0];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.inputmethod.JIM.PreferencesDidChangeNotification"
                                                               object:@"com.apple.JIMPreferences"
                                                             userInfo:/*@{@"JIMPrefLiveConversionKey" : @(0)}*/ // <--コメントアウトを外して使用すること
                                                              options:0];

Warning

sandbox環境では、NSDistributedNotificationはuserInfoにデータを渡すことはできない。sandbox環境では動作しない。

以上のコードでライブ変換の監視/変更が出来るが、sandbox環境では正常動作しない。 sandbox環境で動作するのは変更したタイミングの取得だけ。

“HIToolbox/TextInputSources.h”にあるInputSourceの変更が正規のAPIの可能性があると思い、TISCreateInputSourceListで取得できるリストを見たがそれらしきものはなかった。