Writing an IMAP client for the Mac menu bar

I wanted to read and answer mail without switching to a mail app. Not a full client in a big window, just a popover from the menu bar for the ten-second stuff: anything new? Read it, reply, archive, back to work. That idea escalated into a whole menu bar app (Barista), but the mail client is still the part that took the most work. So this post is about that.

It's not my first go at IMAP. Years ago I started (and never finished) a Geary-style mail client for Linux, so the protocol itself wasn't new to me. What was new: the Mac App Store sandbox, a popover that opens and closes fifty times a day, and Apple's networking stack.

Network.framework, then SwiftNIO

The very first version talked to mail servers through Apple's NWConnection, with TLS from the first byte (port 993 for IMAP, 465 for SMTP). That covers Gmail and iCloud, and it's honestly the nicer way to do it.

But plenty of servers, especially company and self-hosted ones, only offer the older way: STARTTLS on ports 143 and 587. You connect unencrypted, ask the server to switch to TLS, and then do the TLS handshake on the same connection. NWConnection wants TLS decided when you create the connection, and there's no clean way to flip an open one over.

So the networking moved to SwiftNIO. With NIO you can drop a TLS handler into a live connection right after the STARTTLS exchange, which is exactly what the protocol wants. The nice surprise was how little the IMAP and SMTP code above it had to change. The new transport kept the same tiny contract the old one had: send a line, read a line, read exactly N bytes, start TLS.

Two small things I made sure of:

  • After switching to TLS, anything the server sent before the handshake gets thrown away. Otherwise someone in the middle could slip fake responses in while the connection is still unencrypted, and the client would happily trust them.
  • Before trying STARTTLS, the client checks the server actually offers it. If it doesn't, you get a clear message instead of the client quietly carrying on unencrypted.

Don't trust the server's numbers

IMAP tells you up front how big a chunk of data is going to be, like {12345} followed by that many bytes. A broken (or hostile) server could announce {999999999}, or send a line that just never ends. So there are hard limits: 4 MB for a normal line and 64 MB for one of those chunks. Gmail caps attachments at 25 MB, about 34 MB once encoded, so real mail never gets close.

Peeking shouldn't change anything

The message list only asks for the envelope (sender, subject, date) plus a few headers, using BODY.PEEK. The "peek" part matters. A normal fetch marks the message as read, which would mean that just glancing at your inbox marks everything read. When you open a message, it first asks how the message is put together and then downloads only the part it needs, not every attachment.

The message that kept coming back unread

This bug was fixed in 1.1.4. You open a message, Barista marks it read and tells the server. But a refresh might already be on its way, and that refresh asked the server before your change landed. So it comes back saying "still unread", the list gets replaced with the server's version, and a couple of minutes later the message you just read is bold again.

The fix is a small list of "changes the server hasn't confirmed yet". They get re-applied on top of every refresh until the server's own copy agrees, which is the only reliable sign the change stuck. If the server never agrees, the change expires after ten minutes, so a genuinely failed update doesn't keep lying to you forever. It's plain logic with no networking in it, so it's easy to unit-test without a server.

Grouping conversations

Threads are worked out on the Mac. Every message carries its own ID and the IDs of the messages it replies to, and those get linked together, so a chain of replies ends up as one conversation even if some messages in the middle are missing.

Then there's a fallback: messages with the same subject (ignoring the "Re:" and "Fwd:" bits) get grouped too, because lots of senders don't set those reply IDs at all. That's roughly what Gmail does as well. The known downside: a newsletter with the same subject every week turns into one very long thread.

Keeping HTML mail safe

HTML mail is shown with JavaScript always off. Remote content is blocked by default, so tracking pixels don't fire every time you open the popover. You can switch remote content on if you'd rather see the images.

On the sending side, line breaks are stripped out of things like the subject before they're written into the email. Without that, a subject like Hello\r\nBcc: someone@example.com would sneak in an extra header. Outgoing messages also get an ID based on your own email domain. An early version used the app's bundle ID there, and mail providers took that as a spam signal.

Drafts, scheduled sends and snoozed messages are stored encrypted on disk, with the key in the Keychain. Each one gets its own file, so a corrupted snooze list can't take your drafts down with it.

What it doesn't do (yet)

  • No push. IMAP has a way to keep a connection open and get told about new mail right away (IDLE). Barista doesn't use it. It checks every two minutes, and after a failure it retries sooner, backing off from 20 seconds up to 5 minutes. For something living in the menu bar I'm still not sure keeping a connection open per account is worth it. If you have opinions on that, I'd love to hear them.
  • No OAuth sign-in. Gmail and iCloud work with app-specific passwords. Accounts that only allow OAuth, like personal Outlook and most Microsoft 365 accounts, don't work yet.

If you want to build your own

There's plenty of good free material out there. A few that are worth having open:

  • RFC 9051, the current IMAP spec. Most servers still speak the older RFC 3501, so keep that one handy too.
  • RFC 2683, practical advice for people writing IMAP clients, like how not to hammer the server. If you only read one, read this one.
  • RFC 2177 for IDLE (the push thing Barista doesn't do yet) and RFC 7162 for quickly catching up on what changed since you last looked.
  • RFC 5322, RFC 2045 and RFC 2047 for the messages themselves: headers, MIME parts and those =?UTF-8?...?= encoded subjects.
  • MailKit's FAQ. It's for a C# library, but it explains a lot of real-world IMAP and MIME weirdness, whatever language you're using.
  • Gmail's IMAP extensions, if you want Gmail's labels and threads to make sense.
  • Geary, a whole open source mail client to read through, and the one my old Linux attempt was modelled on.
  • Dovecot, a real IMAP server you can run locally and break without touching your own mail.

Bringing a ZX Spectrum +2's Datacorder back from the dead

My grey ZX Spectrum +2 had a really annoying fault. I'd press play, the cassette would turn for a second, and then it would just stop. Nothing dramatic, no grinding, no smoke, just a sad little twitch and then silence. Here's how I tracked it down, the wrong turns along the way, and what finally fixed it.

A grey Sinclair ZX Spectrum +2 128K with its built-in Datacorder cassette deck, in front of a small silver Sony CRT television.
My state-of-the-art setup: the grey +2 and a small Sony CRT.

Can I avoid opening it?

My first thought was to skip the repair altogether: if the deck won't play, just feed the loading audio in from something else, like a phone or a laptop. That's what the EAR socket is for on a lot of 8-bit machines, and it's the usual way around a dead tape deck.

With the grey +2 I am out of luck though. The only audio socket on the back says "Sound", and it's an output. It sends audio out, it can't take a loading signal in. The built-in Datacorder was meant to be the only way in. You can add an EAR socket to the grey +2, but that means opening the case and soldering, which is exactly what I was trying to avoid. And if I was opening the case anyway, I might as well fix the deck properly.

Narrowing it down without tools

Before grabbing a screwdriver, I tried a few things that cost nothing.

  • What works and what doesn't? Fast-forward and rewind were fine, only play failed. So the motor was OK and the problem was specific to play. Play is the hard job: the deck has to pull the tape past the head at a steady speed and keep the take-up reel winding at the same time.
  • The finger test. I tried to stop the spinning flywheel with my finger. It fought back, and spun right up again as soon as I let go. So the motor and the main drive had plenty of power.
  • The pause trick. With a tape in and play pressed, I hit pause and then released it. The deck came back to life, played for a moment and stopped again. Every single time. A dead motor wouldn't come back like that. What I was seeing was the auto-stop doing its job: it cuts the motor when it thinks the take-up reel has stopped turning. So the take-up was stalling, and the auto-stop was (correctly) shutting everything down.

So: motor fine, main drive fine, problem somewhere on the take-up side.

Opening it up

Six screws on the bottom, then lift the top off carefully. The +2's keyboard ribbon cables are famously fragile, so unplug them before you pull the two halves apart.

The +2 case partly open, with coloured wires and a connector still joining the two halves.
Lift slowly: the two halves are still joined by cables until you unplug them.
The opened ZX Spectrum +2: the Amstrad main board in one half of the case, and the Datacorder mechanism and keyboard plate in the other.
The two halves side by side: the main board in one, the Datacorder and the keyboard in the other.

Inside, the Datacorder is its own little self-contained deck: a big flywheel with the drive belt around it, and a small wheel on a pivoting arm (the idler) that swings over to drive the take-up reel when you press play.

Close-up of the Datacorder mechanism: a large black flywheel with a rubber belt running up to the motor pulley, next to the deck's circuit board.
The Datacorder: the big flywheel at the bottom, with the drive belt running up to the motor pulley.

The cleaning attempt (that didn't work)

Old rubber goes shiny and loses its grip, so I started with the cheapest fix there is: cleaning. I cleaned the rubber edge of the idler wheel with isopropyl alcohol until it looked matte, and moved the pivot arm back and forth a few dozen times to loosen up any old, hardened grease. I also cleaned the pinch roller, which was filthy. It took something like twenty cotton buds before they stopped coming out black. (A tip I picked up along the way: rub it with a dry fingertip to tell dirt from perished rubber. Mine came up clean and the rubber was still firm, so the roller was fine.)

Test cassette in, press play... and it turned a little, less than 45 degrees, and stopped. All that cleaning for nothing. A bit of a letdown, but at least I knew it wasn't dirt. Something was worn.

The real culprit: a loose belt

In the end it was almost embarrassingly simple: the big drive belt felt loose.

I'd ruled the belt out early, because fast-forward and rewind worked fine. If the belt was bad, surely those would fail too? Turns out that logic doesn't hold up. Fast winding is an easy job. Play is a heavy one that never lets up: the belt has to keep the flywheel driving the capstan while the tape is dragged past the head the whole time. A stretched, tired belt can handle the easy job and slip on the hard one. And since the take-up is driven off the same flywheel, a slipping belt starves it. The take-up barely moves, the auto-stop sees a stalled reel and cuts out. Exactly what I was seeing. And a 40-year-old belt that's gone slack is apparently the classic +2 loading fault.

There's no tightening a stretched rubber belt, so it had to be replaced.

Finding a belt

Honestly, this was the painful part, way harder than the repair. I am in EU, and getting hold of a £3 belt turned into a quest:

  • One EU shop didn't have it.
  • The best-known UK specialist had it, but since Brexit they only ship within the UK.
  • Another UK shop had closed down.
  • A good UK retailer would ship to me... for £34 of DHL Express, on a £3.49 belt that weighs a few grams.

What finally worked was eBay. A seller listed the belt set with eBay's Global Shipping Programme, so international tracking and customs were sorted up front (no surprise fee at the door), and the total came back down to something sane. If you're in the EU hunting for small retro parts, skip the specialist shops' own websites and look for their eBay listings instead. That's where the international shipping actually works.

A small plastic bag with two black rubber replacement belts, labelled as a Spectrum tape belt pack.
The high-tech sophisticated replacement belts, finally in hand.

The fix

When the belt arrived, I slipped the new long belt over the flywheel and the motor pulley (the easy one, no threading needed), closed it up and pressed play.

It played! The take-up turned smoothly and kept on turning. After all that, one new belt was the whole fix.

Look at this beauty go! Loading from its own Datacorder again, border stripes and all.
The Sony CRT showing the Run the Gauntlet loading screen by Ocean, with the +2 in front of it.
Run the Gauntlet, loaded straight off the tape.

Takeaways

  • Diagnose before you buy anything. The free tests (what works, the finger test, the pause trick) pointed me to the take-up and the auto-stop before I'd spent a penny or touched a screw.
  • Clean it anyway. It didn't fix mine, but it's free, it ruled out dirt, and the deck is happier for it.
  • On a +2 that won't play, suspect the main belt first. Don't overthink it like I did.
  • Buying small parts in the EU since Brexit is a puzzle of its own. eBay's international shipping is often the easiest way through.

One +2, happily loading from its own deck again. Worth all the back-and-forth.

Barista 1.1.4 update has been released

Barista 1.1.4 is a small release with a handful of nice improvements, mostly in Email and Clipboard. 1.1.1, 1.1.2 and 1.1.3 also came out without a post here, so this is a quick round-up of all four, newest first.

Some of these changes came from feedback sent by Barista users, and some had been in the oven for some time.

1.1.4

  • Save email attachments. Until now you could open an attachment but not keep it, since opened files land in a temporary folder that macOS clears. Each attachment now has a save button, and a message with several gets a "Save all" that puts them in one folder (numbering any duplicate names instead of overwriting).
  • See who a message went to. An opened message has a collapsible Recipients row under the subject with To, Cc and Bcc. It's most useful in the Sent folder, where Bcc is usually the part you want to check.
  • Read messages stay read. A message you'd read could quietly turn unread again a few minutes later. There were two causes: marking it read on the server was fire-and-forget, so a failed request went unnoticed, and a refresh that started before the change reached the server could overwrite it with the old state. Now a failed mark is retried, and if it still fails you see a message instead of a silent mismatch. Local changes are also kept and re-applied until the server agrees.
  • Calendar and Weather are always wide. Both now open in their wide dashboard layout, and the setting to switch to a narrow column is gone. For Calendar this is a visible change if you never touched that setting: the popover is wider than before.
  • Bigger clipboard image previews. Copied images show as a preview two rows tall instead of a small thumbnail, in the full history as well as the Images filter. You can switch either list back to small thumbnails.
  • A cleaner clipboard icon. The item count next to the clipboard icon in the menu bar can now be hidden.
  • First-run fix. On a fresh install, Calendar and Reminders could show empty lists until Barista was restarted, and opening the "Next up" event could crash. That's fixed.

1.1.3

  • Act on a whole email conversation from its header row: mark it read or unread, star, archive, snooze or delete every message in the thread at once.
  • Step through messages while reading, with the arrows at each side or ⌥↑ and ⌥↓, without going back to the list.
  • Attachments with accented or non-English filenames now open properly instead of ending up with an unreadable name and no file extension.
  • Unit Converter works both ways. Type into either side and the other one updates.
  • Markdown in the Scratchpad. Turn it on per tab, and headings, bold, lists, quotes and code are styled as you type. The same menu copies a note as rich text.

1.1.2

  • Text Tools gained camelCase, PascalCase, snake_case, kebab-case and CONSTANT case, plus a Clear button and a tidier keypad.
  • Calculator has a scientific mode with parentheses, π and e, trigonometry, square roots, logarithms and more, in degrees or radians. The simple keypad is still the default.
  • Unit Converter has six new categories (data, pressure, energy, power, angle and fuel consumption) and reopens on the last conversion you used.
  • Color Picker can have its own global shortcut, or pick on the first click of its menu bar icon, and it now confirms on screen which HEX value it copied.

1.1.1

  • Email notifications open the email again. In 1.1.0, clicking a new-mail notification often opened Settings instead of the message.
  • Email conversations no longer show buttons on their header row that didn't do anything, and a conversation now looks unread if any message in it is unread, not just the newest one.
  • The Help guide has a screenshot for the Shortcuts settings.

From .hqx and .sit to mountable disk images for Mini vMac

I'm trying to revive my old Macintosh Plus. The plan is to put a BlueSCSI in it, so instead of a real hard drive it boots from an SD card, and I've started building the hard disk image that will go on that card: System 6.0.8 plus the software I want on the machine. I'm putting it together in Mini vMac, an emulator of the same Mac Plus, so I can set everything up on my modern Mac before any of it touches the real hardware.

Most of the old games and apps I wanted come as .hqx or .sit files, and that's where a few evenings went.

Getting files into the emulator

Mini vMac can't see files on your Mac's disk. It only mounts disk images, so dragging a .sit straight onto its window gets you nowhere:

Mini vMac showing an Unsupported Disk Image message: I do not recognize the format of the Disk Image, and so will not try to mount it.
Dragging the A Mind Forever Voyaging .sit onto Mini vMac: "Unsupported Disk Image".

For single files there's a small utility called ImportFl: you run it inside the emulated Mac, drag a file onto the Mini vMac window, and it saves a copy onto the emulated disk. So my first approach was to import the .sit files with ImportFl and expand them inside System 6 with StuffIt Expander, the way you would have done it back in the day.

StuffIt Expander didn't recognise them

That mostly didn't work. StuffIt Expander either didn't recognise the files at all or refused to open them. It took me a while to work out why, and it turned out to be three separate problems:

  • Double wrapping. Some downloads are a StuffIt archive wrapped in BinHex (.hqx, the text encoding used to send Mac files safely over email and the early internet). The StuffIt versions that run on System 6 can't decode BinHex; that needs a separate tool first.
  • Newer StuffIt formats. A lot of archives are saved in the StuffIt 5 format from 1998. No version of StuffIt that runs on System 6 can open those, however you get them onto the machine.
  • Lost file types. Classic Mac files carry a hidden type and creator code, and that's how the old tools know what a file is. ImportFl brings files in without them, so even a perfectly good archive just looks like an unknown document to StuffIt Expander.

The fix: unpack on the host, mount a .dsk

In the end I stopped fighting it inside the emulator and moved all the unpacking to my modern Mac. I wrote a Python script for it, macprep, which turns each download into a raw .dsk image that Mini vMac can mount:

  • Archives are unpacked with unar (the command-line version of The Unarchiver), over and over until nothing packed is left, so a .sit inside an .hqx is no problem. It handles BinHex and every StuffIt version, and on macOS it keeps the resource forks and type and creator codes that old Mac files depend on.
  • Disk images found inside, such as Disk Copy 4.2 images, get converted to raw .dsk.
  • Loose files get packed into a fresh HFS volume, with their folders, resource forks and type and creator codes intact.

It's on GitHub if you want to use it for your own downloads.

Now it's simple: I drag a .dsk onto the Mini vMac window, it mounts as a disk in System 6, and I copy the game or app onto the emulated hard disk. No ImportFl, no StuffIt, no guessing why a file won't open.

The .sit and the converted .dsk on the Mac desktop. The .dsk is dragged onto the Mini vMac window, mounts as AMFV Disk, and opens to show A Mind Forever Voyaging and a System Folder.
The converted .dsk dragged onto Mini vMac mounts straight away as AMFV Disk.
A Mind Forever Voyaging running in Mini vMac, showing the opening text in Communications Mode.
A Mind Forever Voyaging, Infocom's 1985 text adventure, running in Mini vMac.

Next

The next step is to test everything in the emulator, finish the hard disk image, and then copy it to the SD card for the BlueSCSI and try it on the real Plus.

What's new in Barista 1.1

Barista 1.1 is out on the App Store. The 1.0.x updates were mostly fixes and small additions; this is the first release that adds real features. Here are the main ones.

Command palette

Press ⌥⌘K and a Spotlight-style search box appears. Type part of a widget's name, pick it with the arrow keys and Return, and its popover opens. If the widget isn't in your menu bar, choosing it adds it and opens it.

Your own global shortcuts

Settings now has a Shortcuts pane where you can rebind the built-in shortcuts (the Barista bar, Search, Clipboard and the command palette), clear any of them, and add new ones: quick-add a reminder, quick-add a calendar event, or open any specific widget with its own key combination. Restore defaults puts everything back.

Popovers you can unpin

A popover no longer has to stay under its menu-bar icon. Unpin it (the corner button or ⌘P) and it becomes a small floating window with a title strip: drag it anywhere, resize it from the corner, and close it when you're done. It reopens where you left it, and a reset button folds it back under the menu bar.

Notifications open the right thing

Clicking a notification now opens the widget it came from, on the exact item. A new-mail notification opens that email, not just the inbox. Calendar notifications still open the meeting link.

Calendar: a horizontal layout

The Barista calendar in its horizontal layout: the Next up card on top, the day's events and reminders on the left, and the month grid on the right

The calendar has a new Orientation setting. The original vertical layout is still there, and the new horizontal one works as a day dashboard: the day's events and reminders on the left, with an Upcoming section below them, and the month grid on the right. On a wide screen it shows a lot more at a glance.

Email: filters, VIPs and clearer unread mail

The Barista email widget in 1.1: messages from two accounts, with unread messages on a tinted row

The email widget can now show only unread or only starred messages. You can mark senders as VIPs so their mail floats to the top, or mute senders so they disappear from the list. Unread messages are also easier to spot, with a tinted row and separators between messages.

Weather and News

Weather gets a 10-day forecast, a wide layout with the forecast on the right, and next-hour rain and severe-weather alerts where WeatherKit provides them. News gets read and unread state with Mark all read, a saved list for reading later, notifications when a headline matches a keyword, and OPML import and export so you can bring your feeds from another reader.

Fixes

1.1 also fixes a crash when opening the calendar after an event had been deleted elsewhere, some emails showing up as raw encoded text instead of the message, and the Webpage and Email widgets going blank when macOS reclaimed their web renderer. They now recover on their own.

The full list is in the app's What's New window.

Barista is on the Mac App Store.

No sound in Mac games? Check for leftover audio drivers

I originally posted this elsewhere some time ago. The problem is long solved, but I'm putting it here too, as a note for my future self and for anyone who lands here from a search.

The problem

At some point, a few games on my Apple silicon Mac mini stopped playing any sound at all. Other games were completely fine. The ones I noticed it in were Cities: Skylines, StarCraft II and Heroes of the Storm.

The fix

A quick disclaimer first: this is what worked on my machine, so do it at your own risk.

The cause was an extra audio driver that Microsoft Teams installs as a plug-in. I deleted its folder, restarted the Mac, and the sound came back:

/Library/Audio/Plug-Ins/HAL/MSTeamsAudioDevice.driver

While looking into it, I found other people with silent games who fixed them the same way, by removing extra audio drivers. Some of those reports were about the audio driver that Zoom installs, so it's not only a Teams thing. If you have similar symptoms, /Library/Audio/Plug-Ins/HAL/ is a good place to look.

One more thing: removing the driver didn't break Teams. It kept working normally for me, so I'm honestly not sure what the driver is for. It's an optional component in the Teams installer, so you can also just leave it unchecked when you install.

Three problems that surprised me building Barista

In the last post I wrote about what Barista is and why I built it. This one is about the building part: three problems that took far longer to figure out than they should have, and what I learned from each.

1. The app that died before it started

Barista's weather widget uses WeatherKit, Apple's weather service. Adding it was supposed to be a small change: enable the service for the app, add one entitlement, call the API. Instead, the next development build refused to launch. No crash report, no error, nothing in the logs under the app's name. The process was simply killed the moment it started (exit code 137, a SIGKILL).

The cause turned out to be a chain of things I didn't know:

  • As soon as an app carries any com.apple.developer.* entitlement (WeatherKit is one), macOS fully checks its provisioning profile at launch. Without such an entitlement, that check is mostly skipped, which is why every earlier build had worked fine.
  • A development profile only works on the Macs listed in it.
  • On Apple Silicon Macs, the ID the developer portal needs is the Provisioning UDID, not the Hardware UUID. Both appear in System Information, and the Hardware UUID looks like the obvious choice, so that's the one I had registered.

So my own Mac wasn't in the profile, and the system killed the app before any of my code ran. The fix was registering the right ID, which you can find with:

system_profiler SPHardwareDataType | grep "Provisioning UDID"

One more catch: I build Barista with Swift Package Manager and a shell script rather than Xcode, and Xcode normally adds two entitlements (the application identifier and team identifier) behind the scenes. Without them the same check fails too, so they now sit explicitly in the entitlements file.

TestFlight and App Store builds never had the problem, because distribution profiles aren't tied to specific Macs. Which made it even more confusing: the "real" builds worked and only my local ones died.

2. Writing a small email client

I didn't want the email widget to depend on Mail.app, so Barista talks to mail servers itself, using IMAP to read and SMTP to send. The client is written on top of Apple's Network framework and only implements the handful of commands the widget needs.

It only supports servers that accept a TLS connection straight away (ports 993 and 465). The older way, where a connection starts unencrypted and upgrades to TLS partway through (STARTTLS), has no clean path in Apple's NWConnection, and the direct approach is the more secure one anyway. When someone adds a server that only supports STARTTLS, the setup says so plainly instead of failing with a vague error.

A few lessons from the first weeks:

  • Sequence numbers move. IMAP gives each message in a mailbox a position number and a separate, stable ID (the UID). I originally marked messages as read using the position number, remembered from when the list was loaded. But if anything removes a message in between (a server-side filter, Gmail sorting mail into categories, your phone), every position after it shifts, and the server marks the wrong message as read. Switching every action to UIDs fixed it.
  • Not all mail is UTF-8. Older mail and some languages still use legacy encodings like Windows-1252, ISO-8859-7 (Greek) or Shift_JIS. They showed up as =E9 artifacts or garbled text until the client started reading the declared charset of each message and header and decoding with it.
  • Reconnecting is slow. At first, every page of the inbox opened a fresh connection: TCP, TLS handshake, login, fetch, logout. That's 500 to 800 ms per click. Keeping one logged-in connection per account brought paging down to around 250 ms. Later I did the same for opening a message, and added a small in-memory cache so recently fetched messages open instantly.
  • Data indices can surprise you. I buffered server responses in Swift's Data, trimming bytes off the front as they were parsed. After enough trimming, the indices returned by one Data method stopped lining up with what another expected, which crashed on long responses. Switching the buffer to a plain [UInt8] array, whose indices always start at zero, made the problem go away.

3. One process, many widgets

Having all the widgets in one app is the whole point of Barista, but it has a downside: if one widget crashes, the whole app goes down with it. And some crashes can't be caught in Swift. An early example was a text-formatting tool that, given a bare value like 42 instead of a full JSON document, triggered an Objective-C exception that try? doesn't catch.

I can't prevent every crash, but I can stop the same widget from taking the app down again and again. So Barista has a small crash quarantine:

  • Right before a widget does something risky (starting up, or building its popover), Barista writes a small note saying "this widget is doing this now", and clears it afterwards.
  • When the app quits normally, it records a clean exit.
  • On the next launch, if the last exit wasn't clean and a note was still there, Barista knows which widget was busy when things went wrong.

That widget gets switched off before it can run, and a banner in Settings explains what happened, with a button to turn it back on. A popover crash quarantines the widget immediately, since popovers open one at a time and it's clear who was responsible. A crash during start-up needs to happen twice in a row, because many widgets start together, and one unlucky force-quit shouldn't blame an innocent widget.

None of this is visible when things work, which is exactly how it should be. But it means a bug in one widget stays a bug in one widget, instead of an app that won't start.


There were plenty of smaller puzzles too (getting text fields to accept typing inside a menu bar popover deserves its own post), but these three are the ones I'll remember.

Why I built Barista

Ten days ago I shipped Barista. This is a short write-up of what it is, what problem it solves for me, and a few decisions I made along the way.

Small tasks, many times a day

A lot of what I do on my Mac during a day is small: check when the next meeting starts, glance at the weather, convert a unit, paste something I copied an hour ago, see what just landed in my inbox, jot down a note. Each one takes a couple of seconds. But doing it usually means stepping away from what I'm working on: switching to another app, opening yet another window, or hiding everything to get to the widgets on the desktop. And then finding my way back to where I was.

The menu bar is the one place that's always there, above whatever I'm doing. Clicking something there, doing the task in two seconds and carrying on doesn't move a single window or break my train of thought. That's what I wanted for all of those small tasks: click, done, back to work.

One app for all of it

The idea behind Barista is to put those small tasks in the menu bar, in one app. Each widget is one item in the menu bar, and clicking it opens a small popover that behaves like a proper little app rather than a dropdown menu. They all share the same look, spacing and keyboard handling (arrow keys, Return, Esc to close), so once you've used one you know how the others work. Everything is configured from a single settings window.

It started with a handful of widgets and grew to more than 35: calendar, reminders, weather, email, clipboard history, CPU, memory and battery, a calculator, a unit converter, world clocks, a colour picker, and a few small games for when a build is taking its time. A fresh install starts with none of them switched on. You pick the ones you want, so the bar only holds the things you actually reach for.

Calendar

The Barista calendar widget: a month grid with event dots, a "Next up" card, and the day's agenda with a Join button for a video call

The calendar is the widget I use most. It's something I was used to from GNOME, where clicking the clock in the top bar opens a small calendar with your upcoming events. macOS doesn't have that (clicking the clock opens Notification Center instead), and I missed it.

The menu bar shows the next event and its time. Clicking it opens a month grid with dots for busy days, a "Next up" card, and the agenda for whichever day you select. Meetings with a video link get a Join button. It reads from the calendars already set up on your Mac, so there's nothing new to connect.

Email

The Barista email widget: a list of messages across two accounts, with a New button, search and refresh

The email widget was the most work by far. The menu bar shows the unread count. The popover lists recent messages across all your accounts, and you can read, reply and write new mail without leaving it. It talks to mail servers directly, so it doesn't need Mail.app running. Gmail, iCloud, Yahoo and AOL are set up with a few clicks, and other servers work too if they accept a direct TLS connection (the setup runs a quick test and tells you if yours doesn't). It's not trying to replace a full mail client. It's for glancing at what came in and dealing with the quick ones.

Running out of room

With enough widgets turned on, the menu bar fills up quickly, especially next to the notch. So Barista can fold everything behind a single icon. Clicking it drops down a separate Barista bar that holds all the widgets, with its own space independent of the system menu bar. This is set per display, so a laptop screen can stay tidy while an external monitor shows everything.

A few decisions

  • Mac App Store only. Barista runs in Apple's sandbox and uses only public APIs. That ruled out a few things I wanted, such as showing what's playing in any app, a widget for switching Spaces, and a small terminal, because each of them would need private APIs or permissions the sandbox doesn't allow. I'd rather drop a feature than ship something that could break with the next macOS update.
  • Nothing leaves your Mac unless a widget needs it. There's no analytics. Clipboard history and the scratchpad are encrypted on disk, and email passwords live in the Keychain.
  • Light on resources. All the widgets run in one lightweight process. A menu bar app should never be the reason your fans spin up.

What's next

This is the first version, and there's plenty I want to improve. If you try it and something annoys you, I'd genuinely like to hear about it. There's a support page with ways to reach me, and more about the app on its project page.

In the next post I'll write about a few of the technical problems that took far longer to solve than I expected.

Hello, World

#include <stdio.h>

int main(void) {
    printf("Hello, World!\nThis is the first post on the blog.\n");
    return 0;
}