Connect to any bluetooth device and change button color when connected.

I was looking for a button to connect to a bluetooth headphone (or any other bluetooth device), which would change color when it was connected, but I could not find it so far.

So I hacked something together based on the work on AirPods of Duck before.

Basically it is a shell script that will return a value when a device with a certain MAC address is connected, changing the button color. Pressing the button connects/disconnects to the device.

Not connected:

Connected:

Instructions:
In the shell script, change “devicemacaddres” to the MAC address of your bluetooth device (opt+click on the bluetooth icon in your status bar to see it).

This line:
MAC_ADDR=devicemacaddress

In the applescript, replace device name with the name of your bluetooth device.
This line:
tell ( menu item “devicename” of menu 1)

Code is below:

[
  {
    "BTTWidgetName" : "Connect to bluetooth device",
    "BTTTriggerType" : 639,
    "BTTTriggerTypeDescription" : "Apple Script Widget",
    "BTTTriggerClass" : "BTTTriggerTypeTouchBar",
    "BTTPredefinedActionType" : 195,
    "BTTPredefinedActionName" : "Run Apple Script (async in background)",
    "BTTInlineAppleScript" : "activate application \"SystemUIServer\"\rtell application \"System Events\"\r\ttell process \"SystemUIServer\"\r\t\tset btMenu to (menu bar item 1 of menu bar 1 whose description contains \"bluetooth\")\r\t\ttell btMenu\r\t\t\tclick\r\t\t\ttell (menu item \"yourdevice\" of menu 1)\r\t\t\t\tclick\r\t\t\t\tif exists menu item \"Connect\" of menu 1 then\r\t\t\t\t\tclick menu item \"Connect\" of menu 1\r\t\t\t\t\treturn\r\t\t\t\telse\r\t\t\t\t\tclick menu item \"Disconnect\" of menu 1\r\t\t\t\tend if\r\t\t\tend tell\r\t\tend tell\r\tend tell\rend tell",
    "BTTEnabled2" : 1,
    "BTTUUID" : "86953DB3-82B2-4BD1-B533-02D135C80E5F",
    "BTTNotesInsteadOfDescription" : 0,
    "BTTEnabled" : 1,
    "BTTModifierMode" : 0,
    "BTTOrder" : 5,
    "BTTDisplayOrder" : 0,
    "BTTMergeIntoTouchBarGroups" : 1,
    "BTTIconData" : "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAABDmlDQ1BJQ0MgUHJvZmlsZQAAKJFjYGC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zhD2FmMLdy5KegTzGqptU_AqBLtQNAhsNbxd4OrU0UpYAaRDChjP+bzX4irCrsK+wv3C4sJ8wW5gpcJfZRSBNFoensdkzZknPAINiBsl0g9MKabcIm8fBbOYaYaXAf3l6UFglbBBKKzuV1rJ0hmE_wcFn2sYjLbdzgtGSjgE448c0LccNrXBsnhts+NMMmAEzYAbMgBkwA2bADJgBM2AGzIAZMANmwAyYATNgBsyAGTADZsAMmIFqMPD_QwMFmnWABvgAAAAASUVORK5CYII=",
    "BTTTriggerConfig" : {
      "BTTTouchBarAppleScriptString" : "do shell script \"SYSTEM_PROFILER=$(system_profiler SPBluetoothDataType); MAC_ADDR=devicemacaddress; CONNECTED=$(grep -ia6 \\\"${MAC_ADDR}\\\"<<<\\\"${SYSTEM_PROFILER}\\\"|awk '\/Connected: Yes\/{print 1}'); if [[ \\\"${CONNECTED}\\\" ]]; then printf 1; else printf 0; fi\"",
      "BTTTouchBarFreeSpaceAfterButton" : 5,
      "BTTTouchBarOnlyShowIcon" : true,
      "BTTTouchBarBringPlayerToFront" : 0,
      "BTTTouchBarButtonColor" : "63.750000, 63.750000, 63.750000, 255.000000",
      "BTTTouchBarApplyCornerRadiusTo" : 2,
      "BTTTouchBarItemPlacement" : 1,
      "BTTTouchBarAppleScriptStringRunOnInit" : false,
      "BTTTouchBarFontColorAlternate" : "255.000000, 255.000000, 255.000000, 255.000000",
      "BTTTouchBarEmojiHideBackButton" : false,
      "BTTTouchBarHideWhenPaused" : 0,
      "BTTTouchBarAlwaysShowButton" : false,
      "BTTScriptType" : 0,
      "BTTTouchBarButtonWidth" : 100,
      "BTTTouchBarScriptUpdateInterval" : 2,
      "BTTTouchBarItemIconHeight" : 22,
      "BTTTouchBarBatteryHideOnCharging" : false,
      "BTTTouchBarButtonTextAlignment" : 0,
      "BTTTouchBarAlternateBackgroundColor" : "73.000003, 155.000006, 201.000003, 255.000000",
      "BTTTouchBarAppleScriptUsePath" : 0,
      "BTTTBWidgetWidth" : 400,
      "BTTTouchBarDefaultPlayer" : 0,
      "BTTTouchBarColorRegex" : "1",
      "BTTTouchBarItemIconWidth" : 22,
      "BTTTouchBarButtonName" : "Connect to bluetooth device",
      "BTTTouchBarHideWhenNoPlayer" : false,
      "BTTTouchBarFontColor" : "255.000000, 255.000000, 255.000000, 255.000000",
      "BTTTouchBarButtonFontSize" : 15,
      "BTTTouchBarHideCover" : 0,
      "BTTTouchBarButtonCornerRadius" : 6,
      "BTTTouchBarItemPadding" : -13
    }
  }
]