The Current Language flag icon for GoldenChaos doesn't work properly

This works fine if it's in English or French. I also have Japanese layout; whenever I switch to Japanese it doesn't work.

Upon further investigation, I realize it's because they don't have any KeyboardLayout Name. For instance, for Katakana all I see is this

		<dict>
		<key>Bundle ID</key>
		<string>com.apple.inputmethod.Kotoeri</string>
		<key>Input Mode</key>
		<string>com.apple.inputmethod.Japanese.Katakana</string>
		<key>InputSourceKind</key>
		<string>Input Mode</string>
	</dict>

So, there is no KeyboardLayout Name associated with this input mode, unlike, say, Colemak, which is what I am using:

		<dict>
		<key>InputSourceKind</key>
		<string>Keyboard Layout</string>
		<key>KeyboardLayout ID</key>
		<integer>12825</integer>
		<key>KeyboardLayout Name</key>
		<string>Colemak</string>
	</dict>

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.

bump, in the hope that the someone can fix this up for me

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 to my getActiveKeyboardLayout()
on getActiveKeyboardLayout()
set plistPath to "~/Library/Preferences/com.apple.HIToolbox.plist"
try
do shell script "defaults read " & plistPath
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

try
return value of property list item "Input Mode" of pli
end try

end repeat
end tell
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 ):

else if lang contains "Japanese" then
set lang to ":jp:"

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?:


If not, what's the difference? It the --> error number line showing up under the last line?


Open terminal and run this:
defaults read ~/Library/Preferences/com.apple.HIToolbox.plist

Does the AppleSelectedInputSources section look like this:

If not, what's the difference?

are we sure we are looking at the same thing? are you looking at the language widget bar script, or the current language script?

I am talking about

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 ":australia:"
else if activeKbdLayout contains "British" then
set activeKbdLayout to ":uk:"
else if activeKbdLayout contains "Canadian" then
set activeKbdLayout to ":canada:"
else if activeKbdLayout contains "Irish" then
set activeKbdLayout to ":ireland:"
else if activeKbdLayout contains "U.S." then
set activeKbdLayout to ":us:"
else if activeKbdLayout contains "Colemak" then
set activeKbdLayout to ":us:"
else if activeKbdLayout contains "Azeri" then
set activeKbdLayout to ":azerbaijan:"
else if activeKbdLayout contains "Belarusian" then
set activeKbdLayout to ":belarus:"
else if activeKbdLayout contains "Bulgarian" then
set activeKbdLayout to ":bulgaria:"
else if activeKbdLayout contains "Croatian" then
set activeKbdLayout to ":croatia:"
else if activeKbdLayout contains "Czech" then
set activeKbdLayout to ":czech_republic:"
else if activeKbdLayout contains "Danish" then
set activeKbdLayout to ":denmark:"
else if activeKbdLayout contains "Belgian" then
set activeKbdLayout to ":belgium:"
else if activeKbdLayout contains "Dutch" then
set activeKbdLayout to ":netherlands:"
else if activeKbdLayout contains "Estonian" then
set activeKbdLayout to ":estonia:"
else if activeKbdLayout contains "Faroese" then
set activeKbdLayout to ":faroe_islands:"
else if activeKbdLayout contains "Finnish" then
set activeKbdLayout to ":finland:"
else if activeKbdLayout contains "French" then
set activeKbdLayout to ":fr:"
else if activeKbdLayout contains "Austrian" then
set activeKbdLayout to ":austria:"
else if activeKbdLayout contains "German" then
set activeKbdLayout to ":de:"
else if activeKbdLayout contains "Greek" then
set activeKbdLayout to ":greece:"
else if activeKbdLayout contains "Hungarian" then
set activeKbdLayout to ":hungary:"
else if activeKbdLayout contains "Icelandic" then
set activeKbdLayout to ":iceland:"
else if activeKbdLayout contains "Italian" then
set activeKbdLayout to ":it:"
else if activeKbdLayout contains "Romaji" then
set activeKbdLayout to ":jp:"
else if activeKbdLayout contains "Hiragana" then
set activeKbdLayout to ":jp:"
else if activeKbdLayout contains "Katakana" then
set activeKbdLayout to ":jp:"
else if activeKbdLayout contains "Kazakh" then
set activeKbdLayout to ":kazakhstan:"
else if activeKbdLayout contains "Khmer" then
set activeKbdLayout to ":cambodia:"
else if activeKbdLayout contains "Korean" then
set activeKbdLayout to ":kr:"
else if activeKbdLayout contains "2set" then
set activeKbdLayout to ":kr:"
else if activeKbdLayout contains "Latvian" then
set activeKbdLayout to ":latvia:"
else if activeKbdLayout contains "Lithuanian" then
set activeKbdLayout to ":lithuania:"
else if activeKbdLayout contains "Macedonian" then
set activeKbdLayout to ":macedonia:"
else if activeKbdLayout contains "Maltese" then
set activeKbdLayout to ":malta:"
else if activeKbdLayout contains "Norwegian" then
set activeKbdLayout to ":norway:"
else if activeKbdLayout contains "Polish" then
set activeKbdLayout to ":poland:"
else if activeKbdLayout contains "Brazilian" then
set activeKbdLayout to ":brazil:"
else if activeKbdLayout contains "Portugese" then
set activeKbdLayout to ":portugal:"
else if activeKbdLayout contains "Romanian" then
set activeKbdLayout to ":romania:"
else if activeKbdLayout contains "Russian" then
set activeKbdLayout to ":ru:"
else if activeKbdLayout contains "Serbian" then
set activeKbdLayout to ":serbia:"
else if activeKbdLayout contains "Slovak" then
set activeKbdLayout to ":slovakia:"
else if activeKbdLayout contains "Slovenian" then
set activeKbdLayout to ":slovenia:"
else if activeKbdLayout contains "Spanish" then
set activeKbdLayout to ":es:"
else if activeKbdLayout contains "Swedish" then
set activeKbdLayout to ":sweden:"
else if activeKbdLayout contains "Thai" then
set activeKbdLayout to ":thailand:"
else if activeKbdLayout contains "Tongan" then
set activeKbdLayout to ":tonga:"
else if activeKbdLayout contains "Turkish" then
set activeKbdLayout to ":tr:"
else if activeKbdLayout contains "Ukrainian" then
set activeKbdLayout to ":ukraine:"
else if activeKbdLayout contains "Vietnamese" then
set activeKbdLayout to ":vietnam:"
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 to my getActiveKeyboardLayout()
on getActiveKeyboardLayout()
set plistPath to "~/Library/Preferences/com.apple.HIToolbox.plist"

try
do shell script "defaults read " & plistPath
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
try
return value of property list item "Input Mode" of pli
end try
end repeat
end tell
end getActiveKeyboardLayout
set lang to activeKbdLayout

if lang contains "Australian" then
set lang to ":australia:"
else if lang contains "British" then
set lang to ":uk:"
else if lang contains "Canadian" then
set lang to ":canada:"
else if lang contains "Irish" then
set lang to ":ireland:"
else if lang contains "U.S." then
set lang to ":us:"
else if lang contains "Azeri" then
set lang to ":azerbaijan:"
else if lang contains "Belarusian" then
set lang to ":belarus:"
else if lang contains "Bulgarian" then
set lang to ":bulgaria:"
else if lang contains "Croatian" then
set lang to ":croatia:"
else if lang contains "Czech" then
set lang to ":czech_republic:"
else if lang contains "Danish" then
set lang to ":denmark:"
else if lang contains "Belgian" then
set lang to ":belgium:"
else if lang contains "Dutch" then
set lang to ":netherlands:"
else if lang contains "Estonian" then
set lang to ":estonia:"
else if lang contains "Faroese" then
set lang to ":faroe_islands:"
else if lang contains "Finnish" then
set lang to ":finland:"
else if lang contains "French" then
set lang to ":fr:"
else if lang contains "Austrian" then
set lang to ":austria:"
else if lang contains "German" then
set lang to ":de:"
else if lang contains "Greek" then
set lang to ":greece:"
else if lang contains "Hungarian" then
set lang to ":hungary:"
else if lang contains "Icelandic" then
set lang to ":iceland:"
else if lang contains "Italian" then
set lang to ":it:"
else if lang contains "Roman" then
set lang to ":jp:"
else if lang contains "Japanese" then
set lang to ":jp:"
else if lang contains "Katakana" then
set lang to ":jp:"
else if lang contains "Kazakh" then
set lang to ":kazakhstan:"
else if lang contains "Khmer" then
set lang to ":cambodia:"
else if lang contains "Korean" then
set lang to ":kr:"
else if lang contains "Latvian" then
set lang to ":latvia:"
else if lang contains "Lithuanian" then
set lang to ":lithuania:"
else if lang contains "Macedonian" then
set lang to ":macedonia:"
else if lang contains "Maltese" then
set lang to ":malta:"
else if lang contains "Norwegian" then
set lang to ":norway:"
else if lang contains "Polish" then
set lang to ":poland:"
else if lang contains "Brazilian" then
set lang to ":brazil:"
else if lang contains "Portugese" then
set lang to ":portugal:"
else if lang contains "Romanian" then
set lang to ":romania:"
else if lang contains "Russian" then
set lang to ":ru:"
else if lang contains "Serbian" then
set lang to ":serbia:"
else if lang contains "Slovak" then
set lang to ":slovakia:"
else if lang contains "Slovenian" then
set lang to ":slovenia:"
else if lang contains "Spanish" then
set lang to ":es:"
else if lang contains "Swedish" then
set lang to ":sweden:"
else if lang contains "Thai" then
set lang to ":thailand:"
else if lang contains "Tongan" then
set lang to ":tonga:"
else if lang contains "Turkish" then
set lang to ":tr:"
else if lang contains "Ukrainian" then
set lang to ":ukraine:"
else if lang contains "Vietnamese" then
set lang to ":vietnam:"
end if

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.

Wow, somehow I completely missed this. I'll make sure to add this fix in the next version. :slight_smile: