9 Replies Latest reply on Nov 30, 2018 6:17 AM by Andymc7

    Smart Title Case for Table Cells?

    Andymc7 Level 1

      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[i].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[j]) 

              if (k > -1) { 

                  myNewText = myNewText + intCaps[k] + " "; 

                  continue; 

              } else { 

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

                      myNewText = myNewText + theWords[j] + " "; 

                  } else { 

                      myNewText = myNewText + InitCap(theWords[j]) + " "; 

                  } 

              } 

          } 

          theWordRanges[i] = 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[i].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)) 

        • 1. Re: Smart Title Case for Table Cells?
          Andymc7 Level 1

          Anyone??

          • 2. Re: Smart Title Case for Table Cells?
            Manan Joshi Adobe Community Professional

            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

            1 person found this helpful
            • 3. Re: Smart Title Case for Table Cells?
              [Jongware] Most Valuable Participant

              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[c]);
               }
              }
              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[i].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[j])
                if (k > -1) {
                  myNewText = myNewText + intCaps[k] + " ";
                  continue;
                } else {
                  if ((isIn(ignoreWords,theWords[j]) > -1) && (j != 0)) {
                  myNewText = myNewText + theWords[j] + " ";
                  } else {
                  myNewText = myNewText + InitCap(theWords[j]) + " ";
                  }
                }
                }
                theWordRanges[i] = 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[i].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))
              }
              
              • 4. Re: Smart Title Case for Table Cells?
                payalm68947498 Level 2

                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[i].isValid){

                            var cel = table.cells[i].texts;

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

                                cel[j].changecase(ChangecaseMode.titlecase);

                                }

                            }

                        }

                    catch(e){}

                    }

                1 person found this helpful
                • 5. Re: Smart Title Case for Table Cells?
                  Andymc7 Level 1

                  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",]; 

                  • 6. Re: Smart Title Case for Table Cells?
                    Manan Joshi Adobe Community Professional

                    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

                    1 person found this helpful
                    • 7. Re: Smart Title Case for Table Cells?
                      Andymc7 Level 1

                      Perfect! Thank you!

                      • 8. Re: Smart Title Case for Table Cells?
                        Andymc7 Level 1

                        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

                        • 9. Re: Smart Title Case for Table Cells?
                          Andymc7 Level 1

                          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.