Automatically toggle Bluetooth depending on your power source

by postbreak

One of my biggest complaints about using my Macbook in clamshell mode is toggling bluetooth back on to use both my mouse and keyboard. I am one of the many Mac users who attach a large monitor while at my desk and then turn bluetooth off while on battery. Usually when I get home I plug just about every connection I can into my Macbook and then curse at the fact that I have absolutely no way to toggle bluetooth on since my keyboard and mice use it.

So after a year or so of searching I finally got a working script to automatically enable and disable bluetooth depending on the power source. This script is smart in that when it disables bluetooth, you can turn bluetooth back on and the script won’t keep shutting it off. This was one of the biggest hurdles for me to overcome. Luckily I got help. My good friend Bill from cubicle17.com and Chad helped me work this into shape. Specifically Bill helped turn the Applescript into a Bash script which reduced the ram usage exponentially (Applescript gobbled up 11MB vs Bash using 600kb).

The script can be viewed here. Simply download it or copy and paste into a new file and make sure to chmod it so it’s executable.

There is only one requirement to run this script, an application called blueutil. You can download blueutil here, however, I have it mirrored on my site here in case the authors website ever disappears. I am currently looking into a way to implement the code it uses into my script so blueutil is not required. Simply unzip and run the installer. You may have to reboot or log in and out after installation.

So to get this script to run the way it’s designed you need it to run all the time. To do this you should check out the application Lingon. First download it here. Then simply fill out the details like I did below.

bluetoothbash-Lingon

I have had this script running for a few days now and haven’t seen any notable glitches or memory leaks. If you have any problems or questions feel free to leave a comment or send me an email (located in the contact page).