Improved Title Case Macro

Thanks!

I’m happy to update it, but as far as I can tell, the current Title Case script used by the Title Case filter does not actively lowercase anything, so if you start with “C.I.A.” you’ll end with “C.I.A.” unless you lowercase it first.

I can’t see how any non-sentience filter could figure how to uppercase “aci” as an acronym if it was not originally “ACI”, in which case the current filter will leave it uppercase.

I completely forgot that I did in fact run the lowercase filter on the string before the title case filter when using the built-in filter. That would have killed the acronyms, as you point out.

I think the reason for that was to knock down an all caps string so it could be properly capitalized. Which suggests no one approach handles everything. If you have a string with acronyms, you don’t want to lowercase the string. But if you have all caps, you do.

Maybe, though, the code could see if there are any lowercase characters in the string to begin with. The improved version does do that, I see.

I think this takes care of hastags and sign tags:

s{
	([#@])
	( [[:alpha:]] )
}{$1\L$2}xig;

Just add it above the print statement at the end.

Maybe this is what you are suggesting, I'm not sure.
If the entire text is ALL CAPS, then the code should do a lowercase before the Titlecase.

Seems like that should cover most cases. For anything else the user can do a lowercase before calling the Titlecase.

Take these two headlines:

MLB RESULTS FOR FRIDAY
MLB Results For Friday

You can’t do Title Case on the first because it’s already all caps. So you would lowercase it first. But when you do that, you lose the acronym.

You can do Title Case on the second without losing the acronym, though.

The improved Title Case does look at the text to see if it has any lowercase letters. If not, it lowercases the text:

$_ = lc $_ if not /[[:lower:]]/;

That still loses the acronym but at least it edits the text. You just have to restore the acronym. And if the string is not all caps, you’re fine.

(I’d been using the built-in Title Case after a Lowercase and forgot about that Lowercase when I said the improved macro handled acronyms and Title Case didn’t. I was killing the acronyms that Title Case would have left alone.)

Hope the clarifies things.

the only way I can think of to deal with acronyms in all caps is to have a lookup list. I don't know if that is worth the effort or not, or if it would slow down the process too much. It's just an idea.

Maybe it could work like spell check. The system provides a standard set of acronyms (available on the Internet), and then the user could have a custom list he/she could add to.

Well, it’s only an issue with a string that’s all caps so you could deviously avoid selecting any acronym in such a string (since it’s already correct) and process the rest.

I don't think that would work because pretty much every short sequence of letters is an acronym for something. The best you could do would probably be the reverse - if it was not an english word then assume it's an acronym but that would fail for all sorts of things too (eg mad up words, or truncated words or slang or …

Yep, you're right. That's why it was just an idea, an untested idea at that.

What about if the user provided a list when he wanted to check for acronyms?
Maybe that's a separate KM Macro, to use after the Titlecase function/filter.

Yes, that would probably be wise.

In fact, just use the Title Case filter and then apply whatever fixes are required.

Hey JM,

I did this years ago for someone on the BBEdit-Talk list using the Satimage.osax and a very large list of acronyms (about 1400). The thread on BBEdit-Talk is (here).

The downloadable file has the entire list of acronyms included.

The bare AppleScript has ONLY a sample of the large number of acronyms in the downloadable file.

The variable fixedCaseWordList contains words that are to be formatted as they are IN the list and is very easy to add new items to.

As is the script works on the selection in BBEdit, but that's easy enough to change.

It runs nearly instantly on my system.

-Chris


Change case of selected text to title-case v1.01.zip (20.2 KB)

------------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/10/21 17:20
# dMod: 2017/05/18 20:01
# Appl: BBEdit & the Satimage.osax
# Task: Change case of selected text to title-case.
# Libs: None
# Osax: Satimage.osax – http://tinyurl.com/satimage-osaxen
# Tags: @Applescript, @Script, @BBEdit, @Change, @Title, @Case, @Title_Case, @Selected, @Text, @Acronym
------------------------------------------------------------------------------
# SCRIPT REQUIRES INSTALLATION OF THE SATIMAGE.OSAX APPLESCRIPT EXTENSION!
------------------------------------------------------------------------------
set fixedCaseWordList to paragraphs 2 thru -2 of "
a
AFL-CIO
an
and
as
at
but
by
for
from
in
into
it
NAACP
nor
of
on
onto
or
so
the
to
with
"
set lowerCaseWordRegEx to change "(.+)" into "\\\\b\\1\\\\b" in fixedCaseWordList with regexp without case sensitive
set _text to getSelectionOfNamedBBEditWindow(1)
set _text to titlecase _text
set newText to change lowerCaseWordRegEx into fixedCaseWordList in _text with regexp without case sensitive
set newText to change "^(\\w)" into "\\u\\1" in newText with regexp without case sensitive
set newText to change "(\\w)(\\w*)$" into "\\u\\1\\2" in newText with regexp without case sensitive
setBBEditTextSelectionTo(newText)

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on getSelectionOfNamedBBEditWindow(_window)
   tell application "BBEdit"
      if _window = "front" then
         set _window to window (name of front text window)
      else if class of _window = integer or class of _window = text then
         set _window to text window _window
      end if
      
      tell _window to return contents of selection
      
   end tell
end getSelectionOfNamedBBEditWindow
------------------------------------------------------------------------------
on setBBEditTextSelectionTo(_text)
   tell application "BBEdit" to set contents of selection's text to _text
end setBBEditTextSelectionTo
------------------------------------------------------------------------------
2 Likes

Chris, Thanks for showing it can be done, and still be fast. :+1:
Script archived for future use/ref.

I'm trying to adapt your macro to not replace whatever was on the clipboard previously. Copying to a named clipboard doesn't do it. Deleting past clipboard works only if followed by Display Clipboard. With Display Clipboard disabled or removed, the text processed by the Improved Title Case shell script remains on the clipboard, not the previous clipboard contents.

The same is true if I set the shell script to save results to a named clipboard and then paste from there.

Improved Title Case.kmmacros (3.8 KB)

Keyboard Maestro “Improved Title Case” Macro

I think the issue is that there are two clipboard entries to wipe out. I've done that here and it seems to do what you want.

I also updated the Perl script to include the fix for the at sign and hashtag issue Chris pointed out.

Improved Title Case.kmmacros (4.1 KB)

You don't need either a Named Clipboard, nor a Display Clipboard.

Since the macro adds two items to the System Clipboard History, you just need to delete them at the end of the macro, and the item on the System Clipboard (ready to paste) is what you had before you triggered the macro:

Each of these deletes the top (current) Clipboard item.

This is all you need:

##Macro Library   Improved Title Case Ver 2.2 (Del CB 2X) @JMichaelTX


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/0/0ed02a57a44c0bdbd8de0baf51211c5378149c9e.kmmacros">Improved Title Case Ver 2.2 (Del CB 2X) @JMichaelTX.kmmacros</a> (5.0 KB)

**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

<img src="/uploads/default/original/2X/a/a730fa5bdee2394186c5e2474082ec6fc367747d.png" width="459" height="681">

Ah. That was my mistake. I was only deleting past clipboard once. Thank you both!

1 Like

For a built-in locale-sensitive variant:

Copy as Title Case.kmmacros (18.7 KB)


JavaScript for Automation source code

$(Application('Keyboard Maestro Engine').getvariable('itcHeadline'))
    .capitalizedStringWithLocale($.NSLocale.currentLocale).js
1 Like

Hey Folks,

I made a minor bug-fix to my script in post 17 above.

-Chris

New & Improved Title Case

The built-in title case filter in Keyboard Maestro distinguishes between "small words" that should not be capitalized and words that should but it doesn't recognize acronyms or handle hashtags and @ words.

Take, for example, these fake headlines:

The sub-report of the ACI committee
C.I.A. Defeats F.B.I. in Smart Softball match-up
Lightroom CC Includes Significant ACR Revisions
OpticsPro takes on lightroom cc and phase one capture one pro
The #MeToo movement gains momentum
Use @SomeoneElse to contact your congressman

This is how the built-in filter applies capitalization:

The Sub-Report of the ACI Committee
CIA Defeats FBI In Smart Softball Match-Up
Lightroom CC Includes Significant ACR Revisions
OpticsPro Takes on Lightroom Cc and Phase One Capture One Pro
The #MeToo Movement Gains Momentum
Use @SomeoneElse to Contact Your Congressman

John Gruber wrote a Perl routine in 2008 to handle title case which was refined that same year by Aristotle Pagaltzis and put in the public doman (https://gist.github.com/gruber/9f9e8650d68b13ce4d78). It applies capitalization to those same headlines like this:

The Sub-Report of the ACI Committee
C.I.A. Defeats F.B.I. In Smart Softball Match-Up
Lightroom CC Includes Significant ACR Revisions
OpticsPro takes on lightroom cc and phase one capture one pro

The main difference is that when the Gruber/Pagaltzis routine detects an acronym, it leaves it alone, although it won't presume one if it's lowercase.

THIS REVISION

In this revision of the Keyboard Maestro macro, we added UTF-8 handling and changed a forced lower-case initial for hastags and @ words to simply leaving them alone (along with file paths).

We also introduced an exception that strips periods from acronymns.

A commented version of the code accompanies the .zip file to make it easier to eliminate that feature or add other exceptions.

USAGE

Select the text you want to convert and type the Hot Key, which has been set to Shift-Option-T. Your selection will be replaced with the capitalized string.

TitleCase.3.zip (4.7 KB)

3 Likes