2 Replies Latest reply on Oct 14, 2013 2:09 AM by Eugene Tyson

    Smart Title Case - problem with script

    Eugene Tyson Adobe Community Professional & MVP

      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[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("/");
      
      // +++++++ 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))
      }