Switch On/Off Bluetooth

Hello everyone,

Might anyone know if it is possible to switch bluetooth on or off using KM?



I have made a macro for this.
But it uses a commandline utility called blueutil.
I have installed through homebrew. Homebrew requires that Xcode is installed.
So first install homebrew from this link: http://brew.sh
Then run the command brew install blueutil in terminal.

The macro turns on and off with the same hot key.

Keyboard Maestro “Toogle bluetooth” Macro

Toogle bluetooth.kmmacros (4.8 KB)


Hi Jimmy,

Thanks for the helpful reply - will explore after I have explored
reinstalling Xcode :slight_smile:



also see this blog post:


I like this one better. Seems easier to implement. Or maybe it's just me?

Looking at the reply by user Tunes, I edited the Applescript link he referenced to provide a better interface, at least for me. I also did not write a stand alone Applescript app but rather have Keyboard Maestro run the native Applescript.

This Applescript code does not use/launch the Terminal application. It shows a dialog box what it did to Bluetooth and allows you to hit the return key to dismiss the dialog box.

It does require that you download and install the free utility blueutil:

Just using applescript, the following works for me:-

You’re the sh@%! Thank you

1 Like

I've been using one of the above scripts for years now to turn on bluetooth when I plug in my external keyboard, which automatically connects to my trackpad (and turn it off when I unplug etc). But recently it has stopped working.

And the script is:

tell application "Terminal"
   do shell script "/usr/local/bin/blueutil status"
   set _Result to the result
   if _Result is "Status: on" then
      do shell script "/usr/local/bin/blueutil off"
   end if
   if _Result is "Status: off" then
      do shell script "/usr/local/bin/blueutil on"
   end if
end tell

If I open terminal and run the commands it still works. When I run it in Script Editor though Terminal times out on the status command. Or sometimes I get:
Terminal got an error: Error: unable to turn Bluetooth off
Which I've also been getting run using KB.

Does anyone know why this may be happening?

Has something changed on your system? Updated OS or anything like that.

Since the last four years yes. I honestly can't remember when I upgraded to High Sierra, but there was a long period when I wasn't using my external keyboard. But you're right, that's probably it.

I should have asked: How can I get this to work again?

You could split it up into shell scripts in KM. And let KM do the logic. Maybe that will work.

1 Like

Nice thinking! I did that, and it appears that when I run the command from a script, it isn't finding the file, but if I run directly from terminal it is. Is there something I don't understand about paths going on here? Any ideas?

2018-10-24 16:39:15 /var/folders/7f/1ltb3f513f3gdyff4hkkx7jw0000gn/T/Keyboard-Maestro-Script-876E3981-B401-4DCF-9B96-B122E8E8317D: line 1: /usr/local/bin/blueutil status: No such file or directory
 Macro “Turn on Bluetooth when Keyboard plugged in ” cancelled (while executing Execute Shell Script).

Run this command in the terminal to find where blueutil is.
which blueutil

1 Like

Hi there. Did this ever get solved into a working macro? I too have no luck with blueutil and the scripts above. Thanks!

EDIT: Nevermind. I found an easier way to script the On/Off. Works fine using different commands.

Hey @Athena,

Would you please share with the class?  :sunglasses:


Hey Folks,

I'm posting version 2.3.0 of blueutil.

It was compiled on macOS Sierra 10.12.6, so it should run on High Sierra – but I make no bets, since I can't presently test. (Mojave is anyone's guess.)

Like Jimmy I used Homebrew to build it.

blueutil Unix Executable.zip (20.7 KB)

If you want to use it and aren't using Homebrew then I recommend installing it here:


Go to the Finder.

Type G

Paste the path in the resulting field, and hit the Go button.

Copy or move the exe into that directory.

Here's a macro that will add that path to Keyboard Maestro, so the exe is easier to use:

Create a PATH Environment Variable for Keyboard Maestro and Add /usr/local/bin to the Default Path

I also recommend looking at this:

How To Set a Path in Shell Scripts – KM Wiki

The above is for the adventurous.

For the more cautious I've written an AppleScript UI-Script that toggles BlueTooth using the Menu-Extra in your menu bar.

BlueTooth -- Toggle On-Off v1.00.kmmacros (7.2 KB)

It's not as smooth as the blueutil exe, but it's not overly obtrusive either.


1 Like

I've installed blueutil (no homebrew) to /usr/local/bin and used two hotkeys to turn BT on and off. I haven't the experience with KM or scripting to make one key toggle on and off, which would be better.

Bluetooth On.kmmacros (1.7 KB)

It's the same macro to turn off except it's /usr/local/bin/blueutil off as the script and a different hotkey.

Hey @Athena,

Look at the syntax for blueutil by running the following line in the Terminal:

blueutil -h

If you run blueutil -p you'll get either a 1 or a 0 for the on/off status of BlueTooth.

With that as a start you have the means to create a macro that toggles BlueTooth just as you wanted.

Use an Execute a Shell Script action to save the output of the above to a a variable.

Test the variable with a If Then Else action.

Proceed with the correct actions based on your test.

This kind of logic isn't overly difficult once you've done it a few times.

On the other hand in the macro below I've done all the work in the shell except for the notifications.

BlueTooth -- Toggle On-Off -- Using blueutil v1.00.kmmacros (7.6 KB)

Now – let's go back to that help screen for blueutil and take notice of the toggle option.

If I was willing to forego BlueTooth status notifications I could do the whole job with two lines. (One if I want to use the full path to the exe, or if I have an ENV_PATH variable properly set up in Keyboard Maestro.)

export PATH=/usr/local/bin:$PATH;
blueutil -p toggle



1 Like

Sweet! Thanks, Chris!

1 Like