2 Replies Latest reply on Nov 10, 2011 10:19 AM by Tom Tomasko

    [JS CS3] Trying to delete discretionary hyphens from compound words

    Tom Tomasko Level 1

      Hello,

       

      An unsightly typographical error is when a compound word breaks at the end of a line, but not at the hard hyphen. The solution to this is to add a discretionary hyphen before or after (I can't tell if there is a difference here) the hard hyphen. But sometimes one or both of the words in the compound word also have discretionary hyphens, so the words will break anyway.

       

      First I have to eliminate all discretionary hyphens in compound words, then have a function that puts back just one discretionary hyphen at the hard hyphen.

       

      I am having a problem with the first function, which is copied below. It works fine except when the compound word is the very last word in the text and that word has a discretionary hyphen. It would be great if somebocy can point out the little error here.

       

      Thanks,

      Tom

       

      var myDoc = app.activeDocument;

      // Looks for a hyphen with a word before and after.

      var hyphenatedWord = theGrepFinder(myDoc,"\\<\\w+\\>-\\<\\w+\\>");

      for(var g =hyphenatedWord.length-1; g>=0; g--){

          for(var h = hyphenatedWord[g].characters.everyItem().contents.length-1; h >=0; h--){

          if(hyphenatedWord[g].characters.everyItem().contents[h] == 1396983920){//number is discretionary hyphen

              hyphenatedWord[g].characters[h].remove();

                  }//end if

              }//end for h

      }//end for g

        • 1. Re: [JS CS3] Trying to delete discretionary hyphens from compound words
          Peter Kahrel Adobe Community Professional & MVP

          Tom,

           

          I think you kind of got lost with this line: if(hyphenatedWord[g].characters.everyItem().contents[h] == 1396983920): you're asking if every character is a discretionary hyphen, which isnever true. This wotks for me:

           

          for(var g =hyphenatedWord.length-1; g>=0; g--){

             hyphenatedWord[g].contents=hyphenatedWord[g].contents.replace(/\u00AD/g, "");

          }

           

          There's no need to cycle through the found compound words. \u00AD is the discretionary hyphen, so all you need to do is to remove it from the found items. Inserting discretionary hyphens at the beginning of both members of the compound is easiest done separately by a GREP find/replace:

           

          Find: (\w\w+)-(\w\w+)

          Change to: ~-$1-~-$2

           

          Peter

          • 2. Re: [JS CS3] Trying to delete discretionary hyphens from compound words
            Tom Tomasko Level 1

            Thanks Peter!

             

            There is one minor error in your code: /\u00AD /g has a space between D and /.

             

            Also, when I put a discretionary hyphen before the first word and before the second word in the compound word, the compound word will not break at the hard hyphen. But if I put a discretionary hyphen before the second word only neither individual word breaks but it will break at the hard hyphen, which is what I want. I don't understand why it happens this way, but I'll live with it.

             

            Tom