Viktor Strate Kløvedal

MacOS Touch Bar in the Browser

Posted on April 8, 2022

Some Macbook Pro models come with the notorious Touch Bar that Apple quickly abandoned again. After having had such a model for a while now, I have a hard time finding practical use cases for it. One of the reasons why it is not that useful is that apps and the system do not take enough advantage of it.

But then I got an idea that would actually make it useful for me. I wanted to build a browser add-on that allows the native Touch Bar to communicate with websites to let buttons and other Touch Bar controls perform actions on them.

How it works

The project consists of two parts, a regular browser extension and a native binary that controls the Touch Bar. The two parts communicate back and forth using Native Messaging.

The browser extension exposes a bridging API to small JavaScript scripts that are injected into webpages using URL pattern matching. These small scripts are called touchbar packets and are formatted much like Userscripts, with a header describing the name of the packet and one or more URL matches.

The binary is a small CLI app written mostly in Swift, its purpose is to receive configurations for the Touch Bar from the browser extension, update the Touch Bar accordingly and send any user interaction events back to the browser. The binary uses a private system API to show the new Touch Bar on top of the original. Thanks to Touch Bär.

Future of the project

I have afterward completely disabled the dynamic touch bar, which means that the addon doesn’t work with that setup. Also, since Apple is moving away from touch bars, I didn’t want to invest a lot of time in a project that will ultimately become useless. Therefore, I am not planning on working further on the project. I’m still however open to pull requests adding new features or supporting new websites.

But it has been a fun project and very educational for me.