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

Smart Title Case for Table Cells?

Explorer ,
Nov 15, 2018 Nov 15, 2018

Copy link to clipboard

Copied

Hi everyone,

So I really like this script from Eugene Tyson for a better Title Case. However, I would like to know how to run the script for selected table cells only. Can someone help me out?

Any reply much appreciated.

-Andy

//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("\r"); 

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("\r"); 

 

// +++++++ 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

1.2K

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

Community Expert , Nov 27, 2018 Nov 27, 2018

The original is not by Eugene Tyson, it's by Dave Saunders: JavaScripting InDesign: Script of the Day -- Smart Title Case

(It got updated but sadly the link to that version of the script is lost.)

I adjusted it to make it work on a selection inside tables, and added Undo while I was at it. I noticed that due to how it processes the text, the original may delete tables and footnotes from your selection – and possibly more! I've added an alert if you selected a table or footnote, but please keep in

...

Votes

Translate

Translate
Explorer ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

Anyone??

Votes

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 ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

If you want to make this work only on cells and not on any other text you can add the following line to the top of your existing code

if(app.selection[0].constructor.name != "Cell")

     exit()

-Manan

Votes

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 ,
Nov 27, 2018 Nov 27, 2018

Copy link to clipboard

Copied

The original is not by Eugene Tyson, it's by Dave Saunders: JavaScripting InDesign: Script of the Day -- Smart Title Case

(It got updated but sadly the link to that version of the script is lost.)

I adjusted it to make it work on a selection inside tables, and added Undo while I was at it. I noticed that due to how it processes the text, the original may delete tables and footnotes from your selection – and possibly more! I've added an alert if you selected a table or footnote, but please keep in mind there may be more items it can silently remove. I did not try, but think of stuff such as bookmarks, index markers, and hyperlinks. Best use this simple rule: If It Ain't No Plain Text, Do Not Use!

//DESCRIPTION: Converts selected text to title case smartly
// Original by Dave Saunders, 27-Aug-2005
// This version by Jongware, 27-Nov-2018

// * Undo for versions >= CS4.0

// * Process function on selection

// * Selection may be text, table, or table cells

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

if (parseFloat(app.version) < 6)
smartTitleCaps();
else
{
app.doScript (smartTitleCaps, ScriptLanguage.JAVASCRIPT,undefined,UndoModes.ENTIRE_SCRIPT, "Smart Title Caps");
}

function smartTitleCaps()
{
if (app.selection.length && app.selection[0].hasOwnProperty('baseline'))
{
  if (app.selection[0].tables.length || app.selection[0].footnotes.length)
  alert ('Please do not select tables and/or footnotes...')
  else
  processText(app.selection[0]);
} else if (app.selection[0] instanceof Cell || app.selection[0] instanceof Table)
{
  for (c=0; c<app.selection[0].cells.length; c++)
  processText (app.selection[0].cells);
}
}

function processText(mySelection)
{
try {
  myText = mySelection.texts[0].contents;
} catch(e) {
  return;
}

theWordRanges = myText.split("\r");
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)
}
mySelection.texts[0].contents = theWordRanges.join("\r");
}

// +++++++ 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))
}

Votes

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
Explorer ,
Nov 27, 2018 Nov 27, 2018

Copy link to clipboard

Copied

Awesome! Thanks so much!

One more tiny detail I was wondering about...

You see how the "p" remained un-capitalized on "Three-phase"?

How can I change that if I want so that letters after a hyphen also get capitalized?

I tried adding "-p" for the intCaps variable like so, no luck:

var intCaps = ["InDesign","USA","-p",]; 

Votes

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 ,
Nov 27, 2018 Nov 27, 2018

Copy link to clipboard

Copied

Hello Andy,

The current code identifies the words based on group of letters separated by a space and hence your adding the -p to initCaps did not work. What we can do is that when the word is being capitalized we can check for the presence of - and capitalize the character following it. I have modified the code to do just that, replace the version of InitCap method you currently have with the following and it should work.

function InitCap(aWord)

    if (aWord.length == 1) { 

        return (aWord.toUpperCase()); 

    }

     var reg = new RegExp(/-/g)

     var match = reg.exec (aWord)

     while(match)

     {

          aWord = aWord.substring(0,match.index + 1) + aWord.substr(match.index + 1,1).toUpperCase() + aWord.substring(match.index + 2)

          match = reg.exec (aWord)

     }

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

}

-Manan

Votes

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
Explorer ,
Nov 27, 2018 Nov 27, 2018

Copy link to clipboard

Copied

Perfect! Thank you!

Votes

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
Explorer ,
Nov 30, 2018 Nov 30, 2018

Copy link to clipboard

Copied

Oh man..

Well it appears I have another lingering issue. I've tried to fix it myself by modifying initCaps with no luck. Someday I'm gonna teach myself code. In the meantime, can someone please help me to modify the code to capitalize the first letter after a parenthesis and slashes? And, in the future if I have other characters that I'd like to capitalize in this way, is there an easy way to go back into the code and add them?

So in the above table I need "max.", "finished" and "variable" to be capitalized.

Thanks in advance

Votes

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
Explorer ,
Nov 30, 2018 Nov 30, 2018

Copy link to clipboard

Copied

LATEST

On second thought.. I think the best thing to do is NOT capitalize anything within parenthesis, in my particular case. If there is a way to do that I'd appreciate you sharing as well. I know I'm asking a lot, sorry.

Votes

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
Contributor ,
Nov 27, 2018 Nov 27, 2018

Copy link to clipboard

Copied

Hi,

use for loop for multiple cells,

var myDoc = app.documents[0];

var table = myDoc.pages[0].textFrames[0].tables[0];

for(var i = 0; i < table.cells.length; i++){

    try{

        if(app.selection[0].cells.isValid){

            var cel = table.cells.texts;

            for(var j = 0; j < cel.length; j++){

                cel.changecase(ChangecaseMode.titlecase);

                }

            }

        }

    catch(e){}

    }

Votes

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