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.
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.
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?
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).
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.)
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.
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.)
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.
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 )
My computer and phone fight for my AirPods when I take a call so that got me looking for something to turn off Bluetooth. I followed your instructions and everything worked perfectly in Mac OS 12.0.1.
I tried to get some other solutions to work, but yours worked flawlessly.
I'm referring to the Chris Stone post. I used the links to download and install HomeBrew and blueutil.
No problems - everything works fine on a 2019 MBP running OS 12.0.1, Monterey.
Perhaps nobody need another solution to turn Bluetooth on and off, but here's another solution. This solution may need minor changes depending on your screen resolution. It may also need you to move the Bluetooth icon in the system bar as far right as possible (so that its submenu moves to the left rather than the right.)
This solution isn't better than the other solutions, it's simply here to show another technique for educational purposes.