Bug report: Title case fails after smart apostrophe

When I convert text to title case that contains a proper curly apostrophe character, the title case action capitalizes the letter that comes after it. For instance:

I don’t want to go > I Don’t Want to Go [with straight apostrophe]
I don’t want to go > I Don’T Want to Go [with curly apostrophe]

The Improved Title Case macro (Improved Title Case Macro) handles this:

I don’t want to go > I Don’t Want to Go

2 Likes

For a locale-sensitive alternative, if you bind a KM variable with a name like itcHeadline to the string, (for example from the clipboard), you can use this:

JavaScript for Automation source text:

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

( See Improved Title Case Macro )

or in AppleScript (through which access to the Foundation classes is unfortunately more circuitous)

use framework "Foundation"
use scripting additions

tell application "Keyboard Maestro Engine"
    set strText to getvariable ("itcHeadline")
end tell

((current application's NSString's stringWithString:(strText))'s ¬
        capitalizedStringWithLocale_(¬
        (current application's NSLocale's currentLocale()))) as text
2 Likes

Hey Rob,

Unfortunately Apple’s method capitalizes every word and does not follow the rules for proper title caps.

Gruber’s script is supposed to do this properly, but I haven’t scoped it out in quite a while.

-Chris

1 Like

Yes – perhaps it may have some value for other $.NSLocales, though I am not sure to what extent it really adapts to them …

( not a bad first-order approximation for unsophisticated Anglo-Saxons though, and very inexpensive : -)

Thanks for the suggestions. I found this script, which seems like the best option—it handles both small words and contractions correctly.

Here's an action that converts the contents of a variable to title case, if anyone needs one.

Convert Variable "MyText" to Title Case.kmactions (1.8 KB)

1 Like

Just uploaded the third revision of Improved Title Case, which handles some obscure things like hashtags and @ words properly (by leaving them alone this time).

But I’ve also separately included commented code to explain what’s going on in the Perl code. As I understand it the first incarnation of this code by John Gruber is the basis for the title case action in Keyboard Maestro. So this is the same game with a few extensions.