Capitalize first letter of each line

I’ve often got a list that looks like:

apple
pear
plum

I’m trying to use Search and Replace Clipboard Using Regular Expression to find the first character of each line and then capitalize it. I use the following

Search: (?m:^)(.)
Replace: \U$1

This works in other apps like TextWrangler, but in KM the \U doesn’t transform the text, it just inserts the character U.

Hey Tom,

Unfortunately and inexplicably ICU Regular Expression do NOT have a case-change operator…

This is one of the many reasons I keep the Satimage.osax AppleScript Extension on my system.

It’s fully Unicode-aware and makes that sort of thing simple.

set _text to "now is the time for all good men to come to the aid of their country."
set _text to change "\\b(.)" into "\\u\\1" in _text with regexp and case sensitive
--> "Now Is The Time For All Good Men To Come To The Aid Of Their Country."

Keyboard Maestro has some case-change filters (see the Filter Variable/Clipboard actions), but they’re not as versatile as using regular expressions.

Other than using the Satimage.osax your best bet is shelling-out to Perl, Python, Ruby, or JavaScript.

I prefer to use the Satimage.osax for its power and simplicity.

-Chris

1 Like

TextSoap is another option – scriptable, and one can design custom OS X Services for transformations. “Capitalize Words” is one of TextSoap’s built-in actions. Another option is DEVONtechnologies’ WordService, which is a smaller feature set than TextSoap. Both apps generate Services that can be given shortcuts and become invokable in Keyboard Maestro. WordService is $free, TextSoap is an investment – only worth the bucks if you need the features over the long term.

1 Like

You can use this simple JavaScript:

###Example Results

###MACRO:   @RegEx Change to Title Case

~~~ VER: 1.0    2017-05-14 ~~~

####DOWNLOAD:
@RegEx Change to Title Case.kmmacros (2.0 KB)


###JXA Script

var kmeApp = Application("Keyboard Maestro Engine");
var sourceStr = kmeApp.getvariable('Source_String');

sourceStr.replace(/(^|[^a-z0-9])([a-z])/g, 
    function(m,m1,m2,p){ return m1+m2.toUpperCase(); }
    );

1 Like

Thanks a bunch, this is what I needed!