Ever since Alex pointed me to Keyboard Maestro, I’ve been thinking up new ways to use it to shave a few seconds – or minutes – off repetitive, mundane tasks. The latest: switching Bluetooth on and off from the keyboard.

(I’m a bit of an edge case: my desk has an external monitor, an external USB keyboard and a Bluetooth trackpad. And when I plug in my laptop, it’s on top of a stand that makes it a pain to use the MacBook’s trackpad to turn on Bluetooth so it will recognize the external trackpad. Yeah, first-world problem… but it’s my first-world problem.)

Keyboard Maestro lets you trigger all sorts of things from the keyboard, but turning Bluetooth on and off isn’t one of them. (There are lots of other utilities to do this, of course. Quicksilver is a popular free one.) My next stop would normally be AppleScript… but it doesn’t seem to be able to throw the switch on Bluetooth, either.

Open-source software to the rescue, in the person of Frederik Seiffert. He has written a dandy little command-line utility called blueutil that can do one of three things, depending on how you type the command: query Bluetooth’s status (on or off), turn it on, or turn it off.

Unfortunately, unless I’m misreading the documentation, Keyboard Maestro isn’t up to the task of issuing Terminal commands and parsing their results. But AppleScript is.

So having installed blueutil, I wrote this little script to toggle Bluetooth on and off:

tell application “Terminal”
do shell script “/usr/local/bin/blueutil status”
set _Result tothe result
if _Result is “Status: on” then
do shell script “/usr/local/bin/blueutil off”
endif
if _Result is “Status: off” then
do shell script “/usr/local/bin/blueutil on”
endif
endtell

I saved the script, and then created a Keyboard Maestro macro using cmd-shift-option-B to execute it. Voila: a keyboard shortcut that turns Bluetooth on and off.

Mastodon