Watch system change (like bluetooth device just connected)

I'm trying to set a touchbar action to (dis)connect my bluetooth headphones. I'd like to toggle the alternate mode as soon as it (dis)connects, but I couldn't find a clean and efficient way to do it: my current script must check among every known bluetooth devices and see if the desired one is connected, every X seconds. More seconds leads to unresponsive results, less seconds leads to battery drain.

Is there any better way to do this? Didn't find anything here or Github.

Also, I was wondering if BetterTouchTool could use Launchagents to watch system wide changes, like this: https://stackoverflow.com/a/44988170

Example plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

    <key>Label</key>
    <string>Smartcard reader</string>

    <key>ProgramArguments</key>
    <array>
        <string>/Users/USERNAME/Library/Scripts/script.bash</string>
    </array>

    <key>WatchPaths</key>
    <array>
        <string>/Library/Preferences/com.apple.Bluetooth.plist</string>
    </array>

</dict>
</plist>

Looks like it would be very efficient to trigger actions!

Thanks.

I haven't used such launch agents for bluetooth monitoring before, but if this works you could just call BTT from the referenced "script.bash" like this:

osascript -e "tell application \"BetterTouchTool\" to trigger_named \"SomeBluetoothThingConnected\""

This would then trigger a named trigger that can be configured in BTT:

In general I should add more such triggers directly in BTT. I'll work on that.

2 Likes

Thanks for you quick answer. I'm not experienced enough with BTT and bash to get all of this working together, but I'm glad to hear that it will be improved! Thanks again for your amazing work :slight_smile: Loving my touchbar now.