Elegant not sure – perhaps Chris knows an osascript interface ?
If we write some Yosemite JavaScript for Applications, we can interrogate the contents of:
~/Library/Preferences/com.apple.HIToolbox.plist
The most relevant part of which seems to be the AppleSelectedInputSources
key.
(function () {
var a = Application.currentApplication(),
sa = (a.includeStandardAdditions = true, a),
strPath = sa.pathTo('preferences') + '/com.apple.HIToolbox.plist';
return ObjC.deepUnwrap(
$.NSDictionary.dictionaryWithContentsOfFile(strPath)
).AppleSelectedInputSources
})();
Two issues:
-
On my system there seems to be a lag of a few seconds between the input source switch (at the UI level), and the updating of that file. Before the update completes, one can harvest either the previous input method, or simply an empty list.
-
The quickest route to identification depends a bit on which input sources you are using.
On this system, for example, UK English is easily identified by a ּּ"KeyboardLayout Name"
key
[{"InputSourceKind":"Keyboard Layout", "KeyboardLayout Name":"British", "KeyboardLayout ID":2}]
[{"InputSourceKind":"Keyboard Layout", "KeyboardLayout Name":"Hebrew-QWERTY", "KeyboardLayout ID":-18500}]
[{"InputSourceKind":"Keyboard Layout", "KeyboardLayout Name":"French", "KeyboardLayout ID":1}]
While others depend more on exactly which sub-method you are using. Pinyin for Simplified characters for example:
[{"InputSourceKind":"Input Mode", "Bundle ID":"com.apple.inputmethod.SCIM", "Input Mode":"com.apple.inputmethod.SCIM.ITABC"}]
vs Pinyin for Traditional characters:
[{"InputSourceKind":"Input Mode", "Bundle ID":"com.apple.inputmethod.TCIM", "Input Mode":"com.apple.inputmethod.TCIM.Pinyin"}]
The first two also have an ID
key but no "Bundle ID"
, the latter two can be distinguished at the Simplified vs Traditional level by their "Bundle ID"
, and at the specific character-entry method by their "Input Mode"
key.
In short, feasible, but I might hope for a better way : - )
Chris ?