Title Case

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Title Case

Post by JRL » Wed Apr 24, 2013 9:58 pm

Was looking for a way to do "title case", where the first letter of each word is upper case, and couldn't find a good way to do it. This is not perfect. For one thing it will capitalize every word even words that would not normally be capitalized in a title. But it will do the task I need for now.

To use this: Set the script to a hot key in Macro Scheduler. Open some text editor such as Word, Outlook or Notepad and highlight some text. Finally, press the hot key combination. The first character in each word of
the text phrase should become upper case and the highlight should go away.

Hope someone finds this useful.
Dick

Code: Select all

/*
Usage:
Set this script to a hot key.  Highlight some text then press
the hot key combination. The first character in each word of
the text phrase should become upper case.
*/

VBSTART
VBEND

GetClipBoard>vOldData

Press ctrl
Send>c
Release Ctrl

WaitClipBoard
Wait>0.1
GetClipBoard>vData
Let>delimiter=space
Separate>vData,delimiter,vItem

Let>vNewPhrase=
Let>vRegVar_2=
Let>quote="
Let>kk=0
Repeat>kk
  Add>kk,1
  Let>value=vItem_%kk%
  Label>ReValue
  Midstr>value,1,1,vChar
  If>{(%vChar%=%quote%)or(%vChar%="'")or(%vChar%="(")or(%vChar%="[")or(%vChar%="{")or(%vChar%="|")}
    MidStr>value,2,9999999,value
    Let>vNewPhrase=%vNewPhrase%%vChar%
    Goto>ReValue
  EndIf
  VBEval>asc("%vChar%"),vChar
  If>{((%vChar%>64)and(%vChar%<91))or((%vChar%>96)and(%vChar%<123))}
    RegEx>\b[a-z],value,0,vRegVar,vRegNum,1,,vRegRes
  Else
    Let>vNewPhrase=%vNewPhrase%%value%%delimiter%
    Goto>GoToNext
  EndIf
  If>vRegNum=0
    Let>vNewPhrase=%vNewPhrase%%value%%delimiter%
    Goto>GoToNext
  EndIf
  UpperCase>vRegVar_1,vFirstChar
  LowerCase>vRegRes,vLastChars
  If>vRegVar_2<>
    LowerCase>vRegVar_2,vRegVar_2
    Let>vNewPhrase=%vNewPhrase%%vFirstChar%%vLastChars%%vRegVar_2%%delimiter%
  Else
    Let>vNewPhrase=%vNewPhrase%%vFirstChar%%vLastChars%%delimiter%
  EndIf
  Label>GoToNext
  Let>vRegVar_1=
  Let>vRegVar_2=
  Let>vRegRes=
  Let>vFirstChar=
  Let>vLastChars=
Until>kk=vItem_Count

Length>vNewPhrase,vLen
Sub>vLen,1
Midstr>vNewPhrase,1,vLen,vNewPhrase

PutClipBoard>vNewPhrase
WaitClipBoard
Wait>0.1

Press ctrl
Send>v
Release ctrl

Wait>1
PutClipBoard>vOldData

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Thu Apr 25, 2013 2:48 am

This is a good little tool.

http://www.primitivezone.com/primitive- ... anger.html

But what about something like string replace
%SPACE%A
%SPACE%B
%SPACE%C

Etc, sorry I am sure there is a better way, I am just on a mobile at the moment.
FIREFIGHTER

User avatar
Phil Pendlebury
Automation Wizard
Posts: 538
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Thu Apr 25, 2013 8:49 am

Nice one JRL.

As I am sure you guys know, but just in case not...

There is a huge difference between simple word capitalisation and Title Case for articles and Title Case for songs and poems..

The rules for Title Case for articles (or Headline Style) are pretty complex and in places can be ambiguous.

It is something I have tried to get right manually for many years on my blog and I haven't found a tool yet that will automate it properly.

And of course in song titles (also sometimes called Title Case :-/ which I also have to use regularly) you do indeed capitalise every word. (Except nowadays in digital stores, certain words are still required to be lower case.)

It is a minefield.

:-)
Last edited by Phil Pendlebury on Thu Oct 07, 2021 8:27 am, edited 1 time in total.
Phil Pendlebury - Linktree

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Apr 26, 2013 2:25 am

Hi JRL,

That's a handy tool - thanks for sharing!

It reminds me of Clippy which is a free handy clipboard text conversion tool (though Clippy cannot do Title case):
http://www.snapfiles.com/get/clippy.html
Phil Pendlebury wrote:The rules for Title Case for articles (or Headline Style) are pretty complex and in places can be ambiguous.

It is something I have tried to get right manually for many years on my blog and I haven't found a tool yet that will automate it properly.

And of course in song titles (also sometimes called Title Case :-/ which I also have to use regularly) you do indeed capitalise every word.

It is a minefield.
A minefield indeed... check out this Wikipedia link:
https://en.wikipedia.org/wiki/Sentence_ ... ion_titles

There are variations of "Title Case" out there... which rules to follow?
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Title Case

Post by Grovkillen » Thu Oct 07, 2021 7:41 am

According to the PCRE documentation it should be possible to make simple transformations on the match before replacing it. This is NOT working:

Code: Select all

Let>STRING_original=test string to capitalize first letter in each word
Let>REGEX_PATTERN=\b([a-z]{2})
RegEx>REGEX_PATTERN,STRING_original,0,,,1,\U$1\L,STRING_capital_until_no_match_then_lowercase
//TEst STring TO CApitalize FIrst LEtter IN EAch WOrd
Let>REGEX_PATTERN=\b([a-z])
RegEx>REGEX_PATTERN,STRING_original,0,,,1,\u$1,STRING_capital_one_letter
//Test String To Capitalize First Letter In Each Word
Seems like the PCRE2 need to be set to "extended".
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Title Case

Post by JRL » Fri Oct 08, 2021 4:01 pm

Grovkillen,
I know nothing about regex so I can't help you. But I do want to thank you. Back in 2013 when this thread started I was working on an "improvement" to provide a way to allow selected words to remain lower case and thus get closer to a true "Title Case" script. I did not get it working and then forgot about it. You just reminded me.

The code below will work from a list of conjunctions and prepositions that I acquired from who-remembers-where. Any word or phrase that is contained in the list will remain lower case. Feel free to add your own. For example the word "the" is not included in the list and it seems to me that it should be. Phrases should probably be added at the top of the list and individual words below the phrases.

Usage:
Highlight and copy to the clipboard text to be Title Cased.
Run the program.
Paste the updated text.

Hope someone finds this useful.
Dick



Code: Select all

//If there is a word or phrase that you want to remain all lowercase
//simply add it to the "ConjunctionPrepositionList" below.


GetClipBoard>var

//Let>var=you can as long as you do it correctly even though you quit but a while ago the time was wrong
LowerCase>var,var

LabelToVar>ConjunctionPrepositionList,vData
Separate>vData,crlf,Exclusion
Sub>Exclusion_Count,1
Let>vPCTv=%
Let>EX=0
Repeat>EX
  add>EX,1
  Let>value=Exclusion_%EX%
  Separate>var,%space%%value%%space%,ExTest
  If>ExTest_Count>1
    StringReplace>var,%space%%value%%space%,%space%%vPCTv%$#@EXCLU@#$%EX%%vPCTv%%space%,var
    Let>$#@EXCLU@#$%EX%=value
  EndIf
Until>EX=Exclusion_Count

RegEx>.,var,0,Char,Char_Count,0

Let>NewString=
Let>FirstCharFlag=1

Let>kk=0
Repeat>kk
  Add>kk,1
  Let>Value=Char_%kk%
  If>value=",Skipped
  VbEval>asc("%value%"),rrr
  If>%rrr%=32
    Let>FirstCharFlag=1
  EndIf
  If>{(%rrr%>96)and(%rrr%<123)and(%FirstCharFlag%=1)}
    Uppercase>value,value
    Let>FirstCharFlag=0
  EndIf
  Label>Skipped
  Let>NewString=%NewString%%value%
Until>kk=Char_Count

//MDL>NewString
PutClipBoard>NewString
Message>Complete!
Wait>1
Closewindow>Macro Scheduler Message
Exit>


/*
ConjunctionPrepositionList:
as if
as long as
as much as
as soon as
as though
by the time
even if
even though
in case
in order that
provided that
in front of
instead of
on top of
only if
out of
so that
a
after
also
although
and
as
because
before
both
but
either
for
if
lest
neither
nor
once
or
since
so
than
that
though
till
unless
until
when
whenever
where
wherever
whether
while
yet
aboard
about
above
absent
across
after
against
along
alongside
amid
amidst
among
anti
around
as
at
atop
before
behind
below
beneath
beside
besides
between
beyond
but
by
concerning
considering
despite
down
during
except
excepting
excluding
following
for
from
in
inside
into
like
mid
minus
near
next
of
off
on
onto
opposite
outside
over
past
per
plus
regarding
round
save
since
than
through
till
times
to
toward
towards
under
underneath
unlike
until
up
upon
versus
via
with
within
without
*/

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Title Case

Post by Grovkillen » Fri Oct 08, 2021 4:06 pm

Ah, thanks for the JRL!
Let>ME=%Script%

Running: 15.0.24
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts