Set an Icon That Shows on the Touch Bar

I use 2 KEYBOARD LAYOUT (input source in system preference of the mac)

please
i want to set icon that show on the touch bar:
icon that show only when non ENGLISH KEYBOARD LAYOUT is active
when ENGLISH KEYBOARD LAYOUT is active i do not want to see the icon in the touch bar

thanks

This seems a question for BTT.

Touch Bar Widgets ยท GitBook seems to be what you need.

And there are codes to get the current input method:
macos - How to get current input language of the input source - Ask Different

Hi,
I have made it to work in BTT.

Steps:

  • Under "Touch Bar", create an action, and choose "Shell Script/Task".

image

  • Put whatever name you want to "Widget Identifier".
    • I put "Current Input Method".
  • Set how often you want it to check the current input method.
    • I put every 2 seconds.

image

  • Paste the script to the box. You will have to edit the script for your own need (this is the key step!).
    • You may need to open ~/Library/Preferences/com.apple.HIToolbox.plist and see the target string for yourself.
    • I did that, because my keyboards are a bit complicated. See my comments in the code below.
# Most natively installed keyboards can be detected by (U.S., Greek, etc., including the SBL Hebrew keyboard):
NativeKeyboards=$(defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources | grep 'KeyboardLayout Name' | sed -E 's/^.+ = \"?([^\"]+)\"?;$/\1/')

# I also used Squirrel, which have to be checked by:
Squirrel=$(defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources | grep -w 'Input Mode' | head -1 | sed -E 's/^.+ = \"?([^\"]+)\"?;$/\1/')

# I also use Keyman - It does not show the keyboard name. But I have only Hebrew installed in Keyman.
Hebrew=$(defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources | grep -w 'Bundle ID' | tail -n1 | sed -E 's/^.+ = \"?([^\"]+)\"?;$/\1/')

# I commented the English part below, because you don't want it to show when it is English.
# Otherwise, remove the 4 '#'s below, and connect 'el` with 'if`, make them into `elif`.

#if [[ $NativeKeyboards == "U.S." ]]
#then
#  echo ๐Ÿ‡บ๐Ÿ‡ธ
#el
if [[ $NativeKeyboards == "Greek Polytonic" ]]
then
  echo ๐Ÿ‡ฌ๐Ÿ‡ท
elif [[ $NativeKeyboards == "Biblical Hebrew - SIL" ]]
then
  echo "SBL"
elif [[ $Squirrel == "im.rime.inputmethod.Squirrel" ]]
then
  echo ๐Ÿ‡จ๐Ÿ‡ณ 
elif [[ $Hebrew == "keyman.inputmethod.Keyman" ]]
then
  echo ๐Ÿ‡ฎ๐Ÿ‡ฑ
fi

image

  • You may test run the script by clicking the "Run Script Now" button.

  • You may also head to "Common" and do other customizations.

image

  • One option might be useful: "Always show, even if other global actions are hidden."

image


#########################################################################
#########################################################################
#########################################################################

Bonus:

You may use Keyboard Maestro (via AppleScript) to enable/disable the widget (from BTT Forum):

# Disable (replace the dummy UUID with your Widget UUID):
tell application "BetterTouchTool"
	update_trigger "915BE70B-6113-4F81-8841-98334B6E1A1E" json "{\"BTTEnabled2\" : 0}"
end tell

# Enable
tell application "BetterTouchTool"
	update_trigger "915BE70B-6113-4F81-8841-98334B6E1A1E" json "{\"BTTEnabled2\" : 1}"
end tell

Edit (2021/10/04 10:03):

  • We may use the refresh method to replace the disable - enable method:
# (replace the dummy UUID with your Widget UUID):
tell application "BetterTouchTool"
	refresh_widget "915BE70B-6113-4F81-8841-98334B6E1A1E"
end tell
  • We don't need to set the execution interval (just put 0 there. Source):

image