Improved Title Case Macro

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

@mrpasini, I see with your update that you have included the text of your OP, for completeness. Good idea.

I think the time period in which you can update of your OP has passed. Would you like me to replace your OP with this post? OR, at the very least, add a notice/link to your update post so that readers will be aware of the latest update?

Please let me know your preference.

I hadn’t actually thought of updating the original post because there was an ongoing conversation.

I wouldn’t mind disabling the prior two versions in favor of the latest but I didn’t see a way to do that.

The text for the most recent update does restate the issue but adds to it, explaining what’s new, so they aren’t equivalent. But the original is certainly obsolete.

How should I handle this in the future?

So, do you want me to replace your OP with the last update post?

The usual, unofficial, procedure for anything posted in the "Macro Library" is to keep the OP updated to the latest version, unless there is reason to do otherwise. So we usually insert some type of "Updated: date/time" along with Ver: n.nn <Ver Date>

Unfortunately, the original poster is limited to about 1 or 2 months for updating the post.

I have a yellow "updated" image which looks like this:
updated

The source is:

<img src="/uploads/default/original/2X/9/9fa034801953dfe7d1745afcb7087c7530a06e23.gif" width="70" height="17"  alt="updated" title="updated"> 

This utilizes an existing image. Everyone is welcome to use it you like.
Some prefer to just use a heading style, like ### Updated, which looks like this:

Updated

Use whichever you prefer.

9 posts were split to a new topic: Improved Sentence Case Macro

[/quote]

That reply came from my email client (Postbox), which would not know the forum's requirements. Could it be a parsing issue with the forum's software?

I've got a three-liner at the moment that handles the following cases, but the thought occurred to me to run these by you to see if they are what you're dealing with. If not, send me a sample and I'll work it out.

Here are my test cases:

Before: THE FBI SAID IT WAS CONDUCTING FORENSIC TESTING OF TWO-YEAR OLD HASHTAGS.

After: The fbi said it was conducting forensic testing of two-year old hashtags.

Before: This is a sentence... "well, so is this." and this too! but what about this one? that, too.

After: This is a sentence... "Well, so is this." And this too! But what about this one? That, too.

Before: Joseph Grundfest, a commissioner at the S.E.C. in the 1980s and now a professor at Stanford, said he had been contacting current SEC officials and staff to urge them to bring cases, and fast. “it’s more than the extent of the violation,” he said. a spokeswoman for the S.E.C. did not respond to a request for comment.

After: Joseph Grundfest, a commissioner at the S.E.C. in the 1980s and now a professor at Stanford, said he had been contacting current SEC officials and staff to urge them to bring cases, and fast. “It’s more than the extent of the violation,” he said. A spokeswoman for the S.E.C. did not respond to a request for comment.

Yes. I'll do that now.

FYI, this magnificent script is not running correctly since Mojave, unless you disable SIP.

This is according to MacScripters Forum.

I've tried it in Monterey and I get the following error:

Hey Alex,

Quite – Apple deprecated AppleScript scripting additions (OSAX)en in Mojave.

The only way to run them is to disable SIP, and that's not a particularly good idea.

Although I am doing so on Mojave, I will not do that when I move to Monterey (fairly soon).

The good news is that Mark Alldritt (developer of Script Debugger) has come up with a way to use the various Satimage Osaxen on macOS from Mojave to Monterey (and probably beyond – as long as Rosetta is available).

Making the Satimage.osax work on macOS Mojave and beyond.

I'll consider adapting that script to AppleScriptObjC, JavaScript for Automation, Perl, or Python, but it's unlikely that I'll get to it anytime soon.

-Chris