Search code examples
caseautohotkeytitlecapitalize

Intuitive Title Casing



I'm looking for a way to make a script to be more intuitive with Title Casing. What I'm looking for is a script that can recognize symbols (/, -, \, etc.) and typical acronyms (FEMA, CDC, [City]PD, etc.) and apply Title Casing as appropriate. What I currently have is:

SaveVar=%Clipboard%
Clipboard=
ClipWait, 0.5
Send ^c
ClipWait, 0.5
segment = %Clipboard%
; Replace text with text in title case
; Make words following "/" title case
StringUpper, segment, segment , T
; Process exceptions
segment := RegExReplace(segment, "\bA\b", "a")
segment := RegExReplace(segment, "\bAn\b", "an")
segment := RegExReplace(segment, "\bThe\b", "the")
segment := RegExReplace(segment, "\bTo\b", "to")
segment := RegExReplace(segment, "\bAt\b", "at")
segment := RegExReplace(segment, "\bIn\b", "in")
segment := RegExReplace(segment, "\bAs\b", "as")
segment := RegExReplace(segment, "\bAn\b", "an")
segment := RegExReplace(segment, "\bAnd\b", "and")
segment := RegExReplace(segment, "\bBut\b", "but")
segment := RegExReplace(segment, "\bOr\b", "or")
segment := RegExReplace(segment, "\bpdf\b", "PDF")
segment := RegExReplace(segment, "\bllc\b", "LLC")
segment := RegExReplace(segment, "\bdui\b", "DUI")
segment := RegExReplace(segment, "\bAmp\b", "amp")
segment := RegExReplace(segment, "\bPdf\b", "PDF")
segment := RegExReplace(segment, "\bBy\b", "by")
segment := RegExReplace(segment, "\bOf\b", "of")
segment := RegExReplace(segment, "\bFor\b", "for")
segment := RegExReplace(segment, "\b-up\b", "-Up")
; Make first letter uppercase
segment:=RegExReplace(segment, "(\w)(.+)","$U1$2")
; Replace segment text with modified contents of clipboard
Clipboard := segment
ClipWait, 0.5
Send ^v
Sleep 100
Clipboard=%SaveVar%
SaveVar=
return

The problem with this is it will lower case letters following symbols, and all except the first letter of acronyms. Is there a way to make exceptions for letters following symbols, or even apply spacing around the symbol before applying appropriate case?


Solution

  • Underneath the line:

    segment:=RegExReplace(segment, "(\w)(.+)","$U1$2")
    

    add the line:

    segment:=RegExReplace(segment, "(\W)([a-z])","$1$U2")
    

    \W is a symbol and if a lowercase letter follows it, i.e. [a-z] then return the captured symbol, together with the captured letter in uppercase.