Current keyboard layout


It will show keyboard layout for not english only users, and would be awesome to change it by pressing that widget. :slightly_smiling_face:

can somebody implement it?

I've found awesome preset from My preset - Some from GoldenChaos, some from others, some I made myself

set activeKbdLayout to my getActiveKeyboardLayout() # ->, e.g., "U.S."

on getActiveKeyboardLayout()
	
	# Surprisingly, using POSIX-style paths (even with '~') works with 
	# the `property list file` type.
	set plistPath to "~/Library/Preferences/com.apple.HIToolbox.plist"
	
	# !! First, ensure that the plist cache is flushed and that the
	# !! *.plist file contains the current value; simply executing
	# !! `default read` against the file - even with a dummy
	# !! key - does that.
	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
			# Look for (first) entry with key "KeyboardLayout Name" and return
			# its value.
			# Note: Not all entries may have a 'KeyboardLayout Name' key, 
			# so we must ignore errors.
			try
				return value of property list item "KeyboardLayout Name" of pli
			end try
		end repeat
		
	end tell
end getActiveKeyboardLayout

but don't know how to change layout when I press this button

If you're using the one in my preset, that button should have the following script in Predefined Actions

  • edited it to clean up the comments in the script

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 = "Australian" then
set layoutName to "Greek"
else
set layoutName to "Australian"
end if
tell application "System Events" to tell process "SystemUIServer"
tell (1 st menu bar item of menu bar 1 whose description is "text input") to { click , click ( menu 1's menu item layoutName)}
end tell

It's the last few lines that lets you change the layout by pressing the widet.
Change "Australian" and "Greek" to whichever two you want to switch between.
Got to System Preferences -- Input sources and make sure that the 2 you want are on the list. Make sure "Show Input Menu in menu bar" is ticked.
Check the menu bar and make sure the two layouts are first and second on the drop down menu.

Assuming that's all set up, pressing the widget should switch between the 2 input sources. Change the icons to whichever flags (or other symbols) you want.

If you can't get it to work, let me know which layouts you need and I'll see if I can get it to work for me, and then give you the full JSON file.

edit 2: I can also make it cycle between a few different layouts, but making the icons work for that requires saving the icons in a set folder and including it in the script.

Saw your post in GoldenTouch thread. Decided to just go ahead and make it, using the languages you displayed.

Widget 1 - Current Language - Displays current language as a flag, on press opens languages group. Changed it so that the icons are emoji flags
https://share.folivora.ai/sharedPreset/179507fd-325a-462d-a9a1-5d65bae6c43d

Language Groups - Shows different languages/input sources, press each one to switch to that input. Also shows current language on the left. Press to close the group.
https://share.folivora.ai/sharedPreset/5f5d3794-59c9-42a8-9fb9-5d45605f6b1d

Let me know if it doesn't work for you.
If you want it under a modifier button, like opt, make sure you set the languages groupt to the same modifier.

2 Likes

Basing my implementation off of yours. Have you found a way to fix the emoji being vertically off-center? Using emoji is definitely better in this case than using images, I think, but not if the emojis look like this lol.

No idea sorry. I tried to use line breaks either side of the flags in the hopes of it ending up right, but was still offset

Looking good! Will give it a go this evening :slight_smile: thanks for using my languages “mate”!

Is it possible to launch the Language Groups on a longpress on the first widget?

You need to make a named trigger and set it to open Touch Bar group by name. I named the upcoming GC-BTT one "Open Languages", and it just opens the group "Languages". Then in the settings for the current language widget you can add "Open Languages" to the long-press field.

Thanks for your sharing! But it seemed to only work with system input sources. And I have no idea about what's wrong with them? Is a third-parry input source incompatible?
Looking forward to your reply and the following is the script that I have typed in:
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 = "ABC" then

set layoutName to "ABC"

else

set layoutName to "Squirrel"

end if

get layoutName