0 Replies Latest reply on Aug 14, 2013 8:57 AM by tusharde

    Smart Title Case

    tusharde Level 1

      I'm writing a smart title case script. The core of this script is based on the script written by Dave Saunders.

      http://jsid.blogspot.com/2006/06/smart-title-case-revisited.html

       

      When the script is run it changes the paragraph styles applied to the text. I'm new to InDesign scripting, so I can't figure out what might be causing it. Can anyone point me in the right direction?

       

      Screen Shot 2013-08-14 at 8.46.55 AM.png

       

       

      Thanks

      -Tushar

       

       

      Here's my code:

       

       

      //DESCRIPTION: Converts selected text to title case smartly
      // Customize this script by either editing these arrays:
      
      var ignoreWords = ["a", "an", "and", "the", "to", "with", "in", "on", "as", "of", "or", "at", "into",
                           "by", "from", "their", "then", "for", "is", "vitaminwater", "it's"];
      var intCaps = ["PineRidge","InDesign","NJ","UMC", "FCCLA", "SkillsUSA", "d’Oeuvres", "THY", "TV"];
      
      
      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[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("/");
      
      
      runGrep("^.");
      runGrep("(?<=\\.\\s)\\w");
      runGrep("(?<=:)(\\s+)?\\w");
      
      
      // +++++++ Functions Start Here +++++++++++++++++++++++
      
      
      function runGrep(string) {
         app.findGrepPreferences = NothingEnum.nothing;
         app.changeGrepPreferences = NothingEnum.nothing;
      
      
         app.findGrepPreferences = app.changeGrepPreferences = null;
         app.findGrepPreferences.findWhat = string;
         found = app.activeDocument.findGrep (true);
      
      
         if(found.length>0)
         {
            for (j = 0; j < found.length; j++)
            {
               found[j].select();
               app.selection[0].changecase(ChangecaseMode.titlecase);
            }
         }
      
      
         //this below deselects the individual text. And selects the main text frame.
         app.activeDocument.select(NothingEnum.NOTHING);
         app.toolBoxTools.currentTool = UITools.SELECTION_TOOL;
      }
      
      
      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))
      }