Macro to cycle between UPPER and lower case in BBEdit

Does someone have a macro or script to cycle between UPPER and lower case in BBEdit?

The menu looks like this:

I would like to assign a macro to CTRL+= to toggle any selected text between upper and lower case.

I have used BBEdit in the past.... mmm... If I see it correctly, KM can't easily recognize which mode is active. So probably you have to set up two macros (upper/lower). But maybe someone knows better.

@Frankb -- These aren't modes, these are transformations ie "Change the selected text to...", or the whole document if nothing is selected.

@ALYB -- What "rules" do you want to apply? Obviously all lower-case->all upper, and all upper-case->lower, but what about mixed-case selections?

The easy way would be to create your own shortcuts for each operation (BBEdit->Preferences->Menus & Shortcuts) and skip KM altogether. You could also use those shortcuts in keypress actions in KM if working this in to a bigger macro.

Alternatively, BBEdit has brilliant AppleScript support -- which includes change case... making raise case. Easy enough to put in a KM AppleScript action once you've decided about the mixed-case... case.

1 Like

I've a macro that does this.

For it to operate properly you need to first select in BBEdit the text you want to toggle. Then press the hot key (choose whichever you prefer, but I used HYPER X).

The macro works by looking at the first character in the text you have selected: if that character is lower case, then it makes the selected text all upper case; if the character is, conversely, already upper case it switches the selected text to lower case.

If the first character is not alphabetic, the macro does nothing.


Toggle Case in BBEdit.kmmacros (4.5 KB)

Click to see macro

Keyboard Maestro Export


@tiffle very good idea! I can use this system also for other similar problems to toggle with one shortcut.

@Nige_S This is what I meant when I wrote: "KM can't easily recognize which mode is active". Even if "mode" is of course the wrong word :slight_smile:

That’s an equally good idea!

1 Like


In my defence, I've seen (but can't remember the names of) text editors that have CAPS_MODE and lower_mode -- presumably for those who can't get the hang of Shift and/or Caps Lock...

@Nige_S You don't need a defense :wink: :grinning:

Ah, being a beginner, I would use a shortcut to show a palette if I needed all the options BBEdit offers here. Even I know how to do that :joy:


1 Like

In an effort to make a meaningful contribution...

You can write you own BBEdit Text Filters, and run them via Text->Apply Text Filer or assign them a shortcut in BBEdit Preferences. I've never used/written one -- but after a bit of googling, here's an AppleScript filter that follows the rules we've got so far:

  • If text is selected it works on the selection, if no text is selected it works on the complete text of the active document (same behaviour as the BBEdit menu items)
  • If the first character of the selection (or document) is "a" through "z" it uppercases the selection (or document)
  • If the first character of the selection (or document) is "A" through "Z" it lowercases the selection (or document)
  • If the first character of the selection (or document) is anything else it does nothing

Put the following into Script Editor, save as a script to ~/Library/Application Support/BBEdit/Text Filters with your name of choice, assign it a shortcut in BBEdit->Preferences->Menus & Shortcuts (or trigger it via KM) and you're ready to rock:

on RunFromBBEdit(theText)
	set theChar to first character of theText
	considering case
		if theChar is in "abcdefghijklmnopqrstuvwxyz" then
			tell application "BBEdit" to change case theText making raise case
		else if theChar is in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" then
			tell application "BBEdit" to change case theText making lower case
			-- do nothing, no text match
		end if
	end considering
end RunFromBBEdit

You can easily expand this by changing the match strings to include other characters, using ignoring diacriticals so that "å" would be considered a lowercase "a", skip over spaces and punctuation to find the first letter and base your decision on that, etc.

@ALYB -- if you're doing a lot of this sort of thing, take a look at BBEdit's Text Filters and Text Factories. If you can manage a bit of scripting (or, like me, can find something close enough then tweak it to your needs) you'll find them very powerful indeed.

1 Like

Ouch, that hurts.

Just kidding. But you're right, of course, your contribution really does make a difference. Mine doesn't.

No! I meant "More meaningful than my previous contributions to the thread"! Certainly not a comment on your palette idea -- I think I'd started the Text Filter post before you posted, but took a break midway to go and feed the chickens...

The palette idea is a nice one. I prefer a keyboard shortcut because I rarely move to the mouse or trackpad when in BBEdit, but more options are always good.

All good, don't worry. :slight_smile:

You don't have to use the mouse or trackpad for this. Actions of palettes can be assigned single key shortcuts.

For example shortcut to show the palette, then "u" for "Upper Case" or "l" for "Lower Case" etc. and close the palette after one action. I would say this is quick and easy :slight_smile:

Are you talking KM palettes? I haven't got that far in my KM learning yet and -- probably because of that -- I find my mental context-shift when they appear quite jarring. I daresay that would lessen if I used them more (or at all!) but, until then, I find shortcut keystrokes easier.

As always, YMMV -- and the variety is what makes this forum so interesting.

Haha, you don't need palettes if you can write such brilliant scripts :slight_smile:

But yes, I'm talking about KM palettes. You can have hot keys for actions when they are open. These can be normal shortcuts, letters or "special" keys like enter or spacebar etc. and they are shown in the palette. So in this case, "space" would tigger action 2 when the palette is open and then close itself again.


This is so to speak the "standard way", which you never chose, because you can do so much more with your scripts. If you prefer to do without palettes, you can use the very good multipress macros from @noisneil. They do something similar.

As you said :slight_smile:

@Nige_S What rules I want to apply?

lower-case > upper-case
upper-case > lower-case
mixed case > lower-case

I thought this one would be what I needed:

That was a big laugh: it changes camel to CaMeL.

The nice thing about this approach, @Nige_S, is that the selection is maintained, so I can toggle ad libitum.

After writing my post about BBEdit, I realised that I need a toggling macro in other apps (like Safari) too ...

I'll probably end up with an approach that uses your macro, with a check if BBEdit is active. If not, I'll use something like:

When I first wrote my macro to address your question @ALYB I used KM’s Filter actions just as you have but then, because of a mistake I made which made it seem the macro didn’t work I changed from the Filter actions to the Select actions that work directly with BBEdit’s menus.

So I’m glad to see your version which should work in any app and not just BBEdit!

Nice one :clap:

Are you saying the selection wasn't maintained when using the Text Filter approach? Because it is for me. But yes, @tiffle's and your approaches are applicable across many apps and so a lot more generally useful (and more suitable in a forum that's about KM!).

The match condition is going to be interesting, though. Your "rules", as stated, seem to be:

  • If the clipboard contains [A-Z] AND [a-z] make lowercase else
  • If the clipboard does not contain [a-z] make lowercase else
  • Make uppercase

This is much easier to do if your selection is guaranteed to start with a letter (of either case) -- that's the assumption so far -- but otherwise I'm struggling to come up with something concise yet robust...

Credits go to @RochelleBroder with Toggle Word Case Macro