Help with shell script to display custom icon in menu bar

Hello,

I use an app called Swiftbar to display the output of defaults read -g NSPreferredSpellServerLanguage in the menu bar. I work in several languages, and that allows me to quickly determine what language the system language spellcheck is currently set to. I know macOS can do that natively with input sources, but that requires changing the entire keyboard layout, which I do not want to do.

I am trying to take one step further, and instead of just having the current system spellcheck languages be displayed as “en” (English, “fr” (French), or “es” (Spanish), Id like to have Swiftbar display custom png icons which I designed.

GPT offered this solution:

#!/bin/bash

# Run the defaults command and store the output in a variable
language=$(defaults read -g NSPreferredSpellServerLanguage)

# Check if the language is "EN"
if [ "$language" = "EN" ]; then
    # Path to the user-defined PNG icon
    icon_path="/path/to/your/image.png"

    # Display the PNG icon in the menu bar using BitBar
    echo "$(base64 -i "$icon_path") | image=true"
fi

I modified the script to point to the png, but that didn’t work - nothing displayed in menu bar.

Any ideas?

It’s the first time I hear about Swiftbar so I can’t offer any particular help with your solution.

I did notice this in your GPT-created script:

Are you using BitBar (whatever it is) at all?

There’s this webpage:
BitBar | MacMenuBar.com

where the Visit button brings you to a parked domain (although the Collections sidebar has valid links).

Hi - Swiftbar is essentially a more modern - and still actively developed - version of Bitbar. Both function the same way and allow you to customize the menu bar by displaying the output of scripts in it.

I don’t use it, but per the SwiftBar GitHub repository, the value for the image parameter needs to be the base64 encoded image.

I think that’s exactly what he’s doing in the script?

He is getting a base64 encoding, but is setting image=true. Perhaps BitBar was different (and faking out GPT), but from the README I am understanding that the value of image can be light and/or dark encoded images.

oh ok i see yeah i didn’t know what it means

For anyone interested, Swiftbar’s dev wrote me the following working script:

#!/bin/bash

# Get the output of "defaults read -g NSPreferredSpellServerLanguage"
language=$(defaults read -g NSPreferredSpellServerLanguage)

# Define paths to your custom PNG icons
icon_en="/Users/gregorylassale/Documents/Swiftbar icons/Spellcheck Languages/EN.png"
icon_fr="/Users/gregorylassale/Documents/Swiftbar icons/Spellcheck Languages/FR.png"

# Function to convert PNG to base64
function png_to_base64() {
    base64 < "$1" | tr -d '\n'
}

# Check the language and set the appropriate icon path
case "$language" in
    "en")
        icon_path=$icon_en
        ;;
    "fr")
        icon_path=$icon_fr
        ;;
    "es")
        icon_path=$icon_es
        ;;
    *)
        # Default icon or message if the language is not supported
        echo "Unsupported language: $language"
        exit 0
        ;;
esac

# Convert the icon to base64 and display it
icon_base64=$(png_to_base64 "$icon_path")
echo "|image=$icon_base64"