Skip to main content
Community Expert
October 11, 2013
Answered

Smart Title Case - problem with script

  • October 11, 2013
  • 2 replies
  • 931 views

Having a problem here.

When multiple paragraphs are selected the start of a paragraph is set to a lowercase

how can I fix this?

//DESCRIPTION: Converts selected text to title case smartly

var ignoreWords = ["a", "an", "and", "the", "to", "with", "in", "on", "as", "of", "or", "at", "into", "that",

         "by", "from", "their", "then", "for", "are", "not","cannot", "be", "is", "which", "can"];

var intCaps = ["PineRidge","InDesign","NJ","UMC", "FCCLA", "SkillsUSA", "d’Oeuvres", "VAT", "VIES",];

// or by creating text files named ignoreWords.txt and intCaps.txt in the same folder as the script

ignoreWords = getIgnoreFile(ignoreWords);

intCaps = getIntCaps(intCaps);

try {

    myText = app.selection[0].texts[0].contents;

} catch(e) {

    exit();

}

theWordRanges = myText.split("/");

for (var i = theWordRanges.length - 1; i >= 0; i--) {

    theWords = theWordRanges.toLowerCase().split(" ");

    //First word must have a cap, but might have an internal cap

    myNewText = "";

    for (var j = 0; theWords.length > j; j++) {

        k = isIn(intCaps,theWords)

        if (k > -1) {

            myNewText = myNewText + intCaps + " ";

            continue;

        } else {

            if ((isIn(ignoreWords,theWords) > -1) && (j != 0)) {

                myNewText = myNewText + theWords + " ";

            } else {

                myNewText = myNewText + InitCap(theWords) + " ";

            }

        }

    }

    theWordRanges = myNewText.substring(0,myNewText.length - 1)

}

app.selection[0].texts[0].contents = theWordRanges.join("/");

// +++++++ Functions Start Here +++++++++++++++++++++++

function getIgnoreFile(theWords) {

    var myFile = File(File(getScriptPath()).parent.fsName + "/ignoreWords.txt");

    if (!myFile.exists) { return theWords }

    // File exists, so use it instead

    myFile.open("r");

    var importedWords = myFile.read();

    myFile.close();

    return importedWords.split("\n"); // Could filter these, but what's the point?

}

function getIntCaps(theWords) {

    var myFile = File(File(getScriptPath()).parent.fsName + "/intCaps.txt");

    if (!myFile.exists) { return theWords }

    // File exists, so use it instead

    myFile.open("r");

    var importedWords = myFile.read();

    myFile.close();

    return importedWords.split("\n"); // Could filter these, but what's the point?

}

function getScriptPath() {

    // This function returns the path to the active script, even when running ESTK

    try {

        return app.activeScript;

    } catch(e) {

        return e.fileName;

    }

}

function isIn(aList,aWord) {

    for (var i = 0; aList.length > i; i++) {

        if (aList.toLowerCase() == aWord) {

            return i;

        }

    }

    return -1;

}

function InitCap(aWord) {

    if (aWord.length == 1) {

        return (aWord.toUpperCase());

    }

    return (aWord.substr(0,1).toUpperCase() + aWord.substring(1,aWord.length))

}

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

Looks like it is a 'paragraph mark' problem which can be seen as 1st char in some strings.

modify these two lines:

...

     theWordRanges = myText.split("\r");

...

     app.selection[0].texts[0].contents = theWordRanges.join("\r");

...

Jarek

2 replies

Participating Frequently
October 14, 2022

Hello,

How can we apply this script to a specific paragraph style like "Heading 1". 

Right now works on selection.

try {

    myText = app.selection[0].texts[0].contents;

} catch(e) {

    exit();

 

 

Peter Kahrel
Community Expert
Community Expert
October 15, 2022
Jump_Over
Jump_OverCorrect answer
Legend
October 11, 2013

Hi,

Looks like it is a 'paragraph mark' problem which can be seen as 1st char in some strings.

modify these two lines:

...

     theWordRanges = myText.split("\r");

...

     app.selection[0].texts[0].contents = theWordRanges.join("\r");

...

Jarek

Community Expert
October 14, 2013

Worked great - thanks