Language Menu Bar

I am looking at the function embedded inside GoldenChaos - the language bar. I want to add a flag which was not originally included in the script nor the input type. The language I would want to add is Cangjie - Cantonese.

I tried adding:

else if activeKbdLayout contains "Cangjie - Cantonese" then
set activeKbdLayout to "πŸ‡­πŸ‡°"

but seems doesn't work... it just returns empty... kindly please help.

Screenshot 2021-01-21 at 6.29.02 PM

any insight?...

bump...

Try using this script instead:

set keyboardInput to do shell script "defaults read ~/Library/Preferences/com.apple.HIToolbox.plist \
AppleCurrentKeyboardLayoutInputSourceID"

if keyboardInput contains "com.apple.keylayout.US" then
set keyboardInput to ":us:"
else if keyboardInput contains "com.apple.keylayout.British" then
set keyboardInput to ":uk:"
else if keyboardInput contains "com.apple.keylayout.PinyinKeyboard" then
set keyboardInput to ":cn:"
else if keyboardInput contains "com.apple.keylayout.CangjieKeyboard" then
set keyboardInput to ":hong_kong:"
else
set keyboardInput to ":globe_with_meridians:"
end if

If you have more inputs, switch to that input and open terminal and run this line to get its name:

defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleCurrentKeyboardLayoutInputSourceID

Then you can add a new line in the script.

Hi thanks for your reply. Do I just replace the original script with your script? It doesn't seem to work if just use your script and run.

The original script, as extracted from GoldenChaos is:

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 "πŸ‡¬πŸ‡§"
(Truncated code below)

Yeah... Should be direct replacement but our setups could be different. I'm on big sur with m1 so I can't test intel.

You could try to make a new top level trigger and choose type as run applescript. Paste that in and it should run just fine. Let me know...I can help troubleshoot.

So...this is what I did. Decided to add one to my setup. Added shortcut to toggle between inputs too so you can tap it to change language.

  1. I added a new "Top Level Trigger" (middle panel)
  2. Start typing "script" in the search field that popped up in "Select Trigger"
  3. Choose "Apple Script / Javascript Widget"
  4. I put Button Title as (2 lines):
    INPUT
    KB
  5. Copied below code into the script box:

set keyboardInput to do shell script "defaults read ~/Library/Preferences/com.apple.HIToolbox.plist \
AppleCurrentKeyboardLayoutInputSourceID"

if keyboardInput contains "com.apple.keylayout.US" then
set keyboardInput to "US-EN\nπŸ‡ΊπŸ‡Έ"
else if keyboardInput contains "com.apple.keylayout.British" then
set keyboardInput to "GB-EN\nπŸ‡¬πŸ‡§"
else if keyboardInput contains "com.apple.keylayout.PinyinKeyboard" then
set keyboardInput to "CN-PY\nπŸ‡¨πŸ‡³"
else if keyboardInput contains "com.apple.keylayout.CangjieKeyboard" then
set keyboardInput to "HK-CJ\nπŸ‡­πŸ‡°"
else
set keyboardInput to "OTHER\n🌐"
end if

  1. Under [Common] Tab:
  • Button background: Black
  • Item Placement (where you want to put it)
  • Font Size 7, Font Size Second Line 18, Align Center
  • Corner Radius 3
  • HUD Title: Input, Detail: Keyboard Toggle
  1. Under Actions Assigned panel (middle right), I added a "Send Saved Shortcut" of: Control + Option + Spacebar

Hi, thanks for your detail walkthrough... but seems the script is not working properly on my side... I am using M1 on big sur as well. When I tried to do the Run Script in the Script box, it does not return anything from the "Script Results" as well...

Not sure what happened...looks like pasting it here got some characters removed. I tweaked it a little more to look better on mine but here's a screenshot of what I currently have:

Looks like you were missing the double back slashes in the first line. If that doesn't work you can try exporting the preset to a new file and uploading it here. I'll edit and repost.

after adding the double back slashes in the first line it works now! thanks a lot!!

Add this to change input directly from tapping the flag:

And add this to refresh the flag after tapping it so you wont have to wait 5 seconds for it to change:

Get the UUID here (yours will be different from mine) and paste it into the chunk I drew in red: