Case and Keyboard Maestro

The best thing about blogging your little automations is that other people see them and come up with improvements. In an article in iMagazine, Wojtek Pietrusiewicz redid my Cocoa keybindings as Keyboard Maestro macros. The article is in Polish, but you shouldn’t have any trouble getting the gist of it.

Wojtek’s macros are better than my keybindings because they work in any app, even those that don’t honor Cocoa keybindings, like Messages, Twitter, and Slack. Of course, they only work if you have Keyboard Maestro, but if you’re serious about Mac automation, you probably do.

Wojtek does the case changing through Keyboard Maestro filters. I had never looked at Keyboard Maestro’s Filter action before. It has a remarkable list of transformations:

Keyboard Maestro Filters

There are 47 filters in this list, more even than you’ll find in BBEdit’s Text menu.1 I’m not sure why I’d never noticed the Filter action before, but now that I have, I won’t forget it.

While Wojtek’s macros are an improvement on my Cocoa keybindings, they don’t replace the AppleScripts I wrote for BBEdit because they don’t handle the situation in which text is already chosen. Wojtek mentions in his article that he plans to add that ability, but I couldn’t wait. I extended his macros to work like my AppleScripts, but in any app.

Here’s the Lower Case macro. The Upper Case and Capitalize macros are the same except for the filter.

Keyboard Maestro Lower Case macro

The first thing it does is save whatever you have on the system clipboard. We’ll restore that at the end of the macro. It then initializes the clipboard with an empty string—this is necessary to work with the logic of the later steps.

The next step is to simulate a ⌘C keystroke. If there’s text selected, it gets put on the clipboard; if there’s no text selected, the clipboard remains an empty string.

Now we test the contents of the clipboard. If it’s an empty string, we simulate a ⌥⇧← keystroke to select back to the beginning of the word and a ⌘C keystroke to put that text on the clipboard. If the clipboard is not an empty string, we do nothing.

As we leave the macro’s conditional chunk, whatever text we want to change the case of is both selected and on the clipboard. The next two steps change the case of the clipboard and paste it in to replace the selection. Finally, we restore the clipboard to what it was before we started.

Thanks to Wojtek, I now have a set of case-changing macros that work consistently in every app.

  1. Ultimately, I’d say BBEdit wins because a couple of its Text menu items allow you to filter text through any Unix command or script that you’ve written.