• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Smart Title Case - problem with script

Community Expert ,
Oct 11, 2013 Oct 11, 2013

Copy link to clipboard

Copied

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))

}

TOPICS
Scripting

Views

675

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Mentor , Oct 11, 2013 Oct 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

Likes

Translate

Translate
Mentor ,
Oct 11, 2013 Oct 11, 2013

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 14, 2013 Oct 14, 2013

Copy link to clipboard

Copied

Worked great - thanks

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 14, 2022 Oct 14, 2022

Copy link to clipboard

Copied

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();

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 15, 2022 Oct 15, 2022

Copy link to clipboard

Copied

LATEST

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines