I tried to tweak the code so that if there is no KeyboardLayout Name, use the Input Mode field to recognize the language, and display the flag accordingly, but I don't know how to do this with AppleScript. Any help would be appreciated, thanks.
I think I've got it working. Need to tell it to look for both KeyboardLayout Name and Input Mode. Change the first part of the script to:
set activeKbdLayout tomy getActiveKeyboardLayout() on getActiveKeyboardLayout() set plistPath to "~/Library/Preferences/com.apple.HIToolbox.plist" try do shell script "defaults read " & plistPath endtry tellapplication "System Events" repeatwith pli inproperty list itemsof ¬ property list item "AppleSelectedInputSources" of ¬ property list file plistPath try return value ofproperty list item "KeyboardLayout Name" of pli endtry
try return value ofproperty list item "Input Mode" of pli endtry
endrepeat endtell end getActiveKeyboardLayout set lang to activeKbdLayout
Sadly that doesn't work -- I think the script still grabs the last keyboard layout name and just ignores the input mode loop. When I change to a language that only has input mode, it still displays the last language that has keyboard layout name. (e.g. I used Canadian English, so Canadian flag is displayed in my touch bar; however, if I switch to Japanese, the Canadian flag is still displayed in my touch bar, rather than the Japanese flag.
Strange, it's working on mine.
Firstly, check that the script includes the following lines (i.e. says Japanese instead of Hiragana - the Katakana line should work but isn't needed as it's input method includes the word Japanese ):
elseif lang contains "Japanese" then set lang to ""
If that isn't the problem.
Run the script in Script Editor and look at the Replies. Are the last few lines something like this?:
the highlighted script, whose script is reproduced here. (I added your fix, but that didn't work; so I took that code out again)
set activeKbdLayout to my getActiveKeyboardLayout()
on getActiveKeyboardLayout()
set plistPath to "~/Library/Preferences/com.apple.HIToolbox.plist"
try
do shell script "defaults read " & plistPath & " dummy"
end try
tell application "System Events"
repeat with pli in property list items of ¬
property list item "AppleSelectedInputSources" of ¬
property list file plistPath
try
return value of property list item "KeyboardLayout Name" of pli
end try
end repeat
end tell
end getActiveKeyboardLayout
if activeKbdLayout contains "Australian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "British" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Canadian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Irish" then
set activeKbdLayout to ""
else if activeKbdLayout contains "U.S." then
set activeKbdLayout to ""
else if activeKbdLayout contains "Colemak" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Azeri" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Belarusian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Bulgarian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Croatian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Czech" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Danish" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Belgian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Dutch" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Estonian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Faroese" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Finnish" then
set activeKbdLayout to ""
else if activeKbdLayout contains "French" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Austrian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "German" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Greek" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Hungarian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Icelandic" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Italian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Romaji" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Hiragana" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Katakana" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Kazakh" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Khmer" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Korean" then
set activeKbdLayout to ""
else if activeKbdLayout contains "2set" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Latvian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Lithuanian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Macedonian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Maltese" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Norwegian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Polish" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Brazilian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Portugese" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Romanian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Russian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Serbian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Slovak" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Slovenian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Spanish" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Swedish" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Thai" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Tongan" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Turkish" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Ukrainian" then
set activeKbdLayout to ""
else if activeKbdLayout contains "Vietnamese" then
set activeKbdLayout to ""
else if activeKbdLayout is missing value or activeKbdLayout is "Hide Input Source Name" or activeKbdLayout is "Open Keyboard Preferences…" then
set activeKbdLayout to ""
end if
Just asking because you keep saying "lang contains" blah blah, which is the script under the Language widget bar (Script: Language 1, Script: Language 2, and so forth). I am not talking about those guys.
Sorry, forgot I changed the wording in my Current Language script.
Try this:
set activeKbdLayout tomy getActiveKeyboardLayout() on getActiveKeyboardLayout() set plistPath to "~/Library/Preferences/com.apple.HIToolbox.plist"
try do shell script "defaults read " & plistPath endtry tellapplication "System Events" repeatwith pli in *property list items of ¬ property list item "AppleSelectedInputSources" of ¬ property list file plistPath try return value ofproperty list item "KeyboardLayout Name" of pli endtry try return value ofproperty list item "Input Mode" of pli endtry endrepeat endtell end getActiveKeyboardLayout set lang to activeKbdLayout
if lang contains "Australian" then set lang to "" elseif lang contains "British" then set lang to "" elseif lang contains "Canadian" then set lang to "" elseif lang contains "Irish" then set lang to "" elseif lang contains "U.S." then set lang to "" elseif lang contains "Azeri" then set lang to "" elseif lang contains "Belarusian" then set lang to "" elseif lang contains "Bulgarian" then set lang to "" elseif lang contains "Croatian" then set lang to "" elseif lang contains "Czech" then set lang to "" elseif lang contains "Danish" then set lang to "" elseif lang contains "Belgian" then set lang to "" elseif lang contains "Dutch" then set lang to "" elseif lang contains "Estonian" then set lang to "" elseif lang contains "Faroese" then set lang to "" elseif lang contains "Finnish" then set lang to "" elseif lang contains "French" then set lang to "" elseif lang contains "Austrian" then set lang to "" elseif lang contains "German" then set lang to "" elseif lang contains "Greek" then set lang to "" elseif lang contains "Hungarian" then set lang to "" elseif lang contains "Icelandic" then set lang to "" elseif lang contains "Italian" then set lang to "" elseif lang contains "Roman" then set lang to "" elseif lang contains "Japanese" then set lang to "" elseif lang contains "Katakana" then set lang to "" elseif lang contains "Kazakh" then set lang to "" elseif lang contains "Khmer" then set lang to "" elseif lang contains "Korean" then set lang to "" elseif lang contains "Latvian" then set lang to "" elseif lang contains "Lithuanian" then set lang to "" elseif lang contains "Macedonian" then set lang to "" elseif lang contains "Maltese" then set lang to "" elseif lang contains "Norwegian" then set lang to "" elseif lang contains "Polish" then set lang to "" elseif lang contains "Brazilian" then set lang to "" elseif lang contains "Portugese" then set lang to "" elseif lang contains "Romanian" then set lang to "" elseif lang contains "Russian" then set lang to "" elseif lang contains "Serbian" then set lang to "" elseif lang contains "Slovak" then set lang to "" elseif lang contains "Slovenian" then set lang to "" elseif lang contains "Spanish" then set lang to "" elseif lang contains "Swedish" then set lang to "" elseif lang contains "Thai" then set lang to "" elseif lang contains "Tongan" then set lang to "" elseif lang contains "Turkish" then set lang to "" elseif lang contains "Ukrainian" then set lang to "" elseif lang contains "Vietnamese" then set lang to "" endif
Thanks! I forgot to reply to this. I didn't see any need for "set lang to activeKbdLayout", so I took that out, and changed lang into activeKbdLayout. Now it's all working fine. Great work.