Switcheroo, bluetooth keyboard and mouse across Macs made easy

Companies providing a Macbook as work laptops do a great favor to themselves and the people receiving them for productivity, years of usage have made me really fond of Mac OS versus the amount of maintenance I was demanded by my Windows workstations making it almost a second job on its own.

Still even though being pricy objects it’s not a good idea to think of mixing up your work device with your personal stuff, on one hand business security of course as you might download or do stuff that might endanger the biz, on the other hand you cannot really expect privacy on a machine you do not own now, would you?

For such reason I, as many others, did resort after being delighted by the Mac experience to get a personal one for more private and mundane purposes, so to benefit of the zero maintenance and it-just-works™ life.

While working at home though, you may want to up the game a bit by plugging into a bigger desktop setting with external peripherals such as monitor, mouse and keyboard, now as I want as less clutter on my desk as possible I prefer to use bluetooth ones, and here’s where things with Mac start to be less “ideal”.

Each time you want to “switch” the Mac to which your mouse and keyboard are connected, you’ll have to summon the bluetooth settings windows, unpair manually both devices and and re-pair them on the other machine each time you pass from one laptop to the other, making this an 8 clicks operation.

 

What if this was reduced to just two (2) clicks?

Introducing: Switcheroo

So first things first, the base of this script is to be credited to Ophir Stern on apple.stackexchange, to which I’ve added a bit of logic and variables to check which directory the tool is available, as it can differ among machines even using the same install method, and because we want to use it on two devices, I’ve made it into a function to accept the device address as argument so its easier to setup, replacing automatically the address separators from – (dashes) to : (colons) as required by blueutils.

Install either via brew or other package manager the blueutilpackage on both Macs, then run which blueutil and paste the respective paths in the homePath and workPath variables.

Then run blueutil --paired on the computer to which the peripherals are currently connected to get their physical addresses, and replace the dashes (-) with colons (:) then paste them in keyboardAddress and mouseAddressreplacing the placeholders,

Now launch Automator and select to create a new Application, then among actions search for “shell” and add the “Execute shell script” action, paste the amended script (or copy/paste first and amend in the IDE).

You’ve most likely noticed the presence of various emojis in the code, being a shell script we wouldn’t have any feedback while it’s running, to be aware that it has been launched and most importantly ended correctly or not we can use notifications, so insert two of them before and after the script block, then most importantly add between the script and the last notification the step “Set variable value” that will pass to the last notification the result of the echo commands.

Now you can hit Cmd+S to save and create the Application, and name it Switcheroo.

The freshly created application has the Automator icon though, to make it more recognizable you can single click to select it, hit Cmd+i then drag on the small icon on top left of the info window the following icon to override it.

Now move the application into a directory like your Dropbox or iCloud docs dir, then drag the application to the Dock bar, finally send a copy of the Switcheroo application to the other Mac as well and add it to the Dock bar there too.

Finally, on the Mac where the mouse and keyboard are currently connected, you can one click the Switcheroo icon in your Dock to have both devices instantly unpaired, and one second after you can click again the Switcheroo icon on the other Mac Dock to have both connected on the other laptop.

From 8 clicks to 2. 😎

Notes on stalling and timeout

Unfortunately the bluetooth API may be unstable from time to time and some hiccups may happen in case the device does not happen to be on discovery mode or is not turned on at all, the command blueutil --pair in particular may hang indefinitely when the device is not found as it has not internal timeout nor a way to specify one.

There would be the handy timeout tool part of the coreutils library available via brew, however when used in conjunction with the command blueutil --pair , it results in the Mac asking a prompt confirm for the device to be connected which would kill the purpose of having fewer click.

Using the timeout command against bash functions within Automator proved next to impossibile, so in order to avoid not receiving any feedback when the script blocks, it will self terminate within 15 seconds.

https://sresc.io/1fpN

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.