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 tomy getActiveKeyboardLayout() on getActiveKeyboardLayout() set plistPath to "~/Library/Preferences/com.apple.HIToolbox.plist" try do shell script "defaults read " & plistPath & " dummy" 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 endrepeat endtell
end getActiveKeyboardLayout if activeKbdLayout = "Australian" then set layoutName to "Greek" else set layoutName to "Australian" endif tellapplication "System Events" totellprocess "SystemUIServer" tell (1 stmenu bar itemofmenu bar 1 whose description is "text input") to { click , click ( menu 1's menu item layoutName)} endtell
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.
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.
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 tomy getActiveKeyboardLayout()
on getActiveKeyboardLayout()
set plistPath to "~/Library/Preferences/com.apple.HIToolbox.plist"