Switch On/Off Bluetooth

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?
:slight_smile:

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:

-Chris

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:

/usr/local/bin/

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.

-Chris

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

Enjoy.

-Chris

1 Like

Sweet! Thanks, Chris!

1 Like

A post was split to a new topic: Does KM support multiple icons for a macro on a palette?

Hi @ccstone

Is there anyway of finding out which version of blueutil is currently on my system?

Cheers,
Damian

Hey Damian,

Run the following script in the Terminal:

blueutil --version

-Chris

1 Like

Thanks Chris. Oddly, that just gives me this:
Usage: blueutil [status|on|off]

I'm really unfamiliar with terminal. I just do what I'm told in it! If I install a new version of blueutil, will that overwrite the old one, or is there an uninstall. I do have homebrew installed, if that makes a difference. I should really go and do some basic terminal 101 research I suppose.

Hm... Sounds like your version is pretty old then.

Try this:

blueutil -h

it depends upon how you install it.

Not per-se, however HomeBrew has an uninstall feature for items installed with it.

It does.

Try running this in the Terminal to update HomeBrew itself and any installed recipes.

brew -v update && brew -v upgrade

-Chris

1 Like

Thank you for AppleScript-only. Blueutil doesn't work with Catalina (although maybe the recompiled version does...glad just to have text to handle this rather than a file :slight_smile: )

1 Like