4 Replies Latest reply on Mar 2, 2014 2:59 AM by Laubender

    Finding and Changing "first word character syle" in text

    cuneytoral

      I want to search spesific word and only replacing first founded word character style in text. Like this :

      "Transfer to Istanbul Airport for morning flight to Adana.  Meet and transfer to Antioch , the place where the followers of Jesus were first called Christians. Here you will visit the Church of St. Peter, a grotto discovered by the Crusaders and reputed to be the cave church where early Christians met in secret. At the Mosaic Museum in Antioch you will see an unusually rich collection of mosaics from this area. Following your visit at the museum, you will go to the Seleucia Pieria, the ancient port town of Antioch from which Paul and Barnabas set out on their first journey and where Paul returned afterwards.  Finaly, you will check into the hotel in Antioch for dinner and overnight. (B,D)"

       

      This paragraph contains 4 Antioch word. And I want to make BOLD first one.

       

      This is my code but this code change all of them to bold. Can anyone help me?

       

      var myDocument = app.documents.item(0);
      
      // Clear old preferences
      app.findTextPreferences = NothingEnum.nothing;
      app.changeTextPreferences = NothingEnum.nothing;
      
      //Set the find options.
      app.findChangeTextOptions.caseSensitive = false;
      app.findChangeTextOptions.includeFootnotes = false;
      app.findChangeTextOptions.includeHiddenLayers = false;
      app.findChangeTextOptions.includeLockedLayersForFind = false;
      app.findChangeTextOptions.includeLockedStoriesForFind = false;
      app.findChangeTextOptions.includeMasterPages = false;
      app.findChangeTextOptions.wholeWord = false;
      
      // This CharacterStyle set to BOLD FONT STYLE
      app.changeTextPreferences.appliedCharacterStyle = myCharacterStyle;   
      
      app.findTextPreferences.findWhat = "Antioch";
      app.changeTextPreferences.changeTo = "Antioch";
      myDocument.changeText();
      
        • 1. Re: Finding and Changing "first word character syle" in text
          Ronald63 Level 4

          Hi,

           

          Try this ...

          var myDocument = app.documents.item(0);

          // Clear old preferences
          app.findTextPreferences = NothingEnum.nothing;
          app.changeTextPreferences = NothingEnum.nothing;

          //Set the find options.
          app.findChangeTextOptions.caseSensitive = false;
          app.findChangeTextOptions.includeFootnotes = false;
          app.findChangeTextOptions.includeHiddenLayers = false;
          app.findChangeTextOptions.includeLockedLayersForFind = false;
          app.findChangeTextOptions.includeLockedStoriesForFind = false;
          app.findChangeTextOptions.includeMasterPages = false;
          app.findChangeTextOptions.wholeWord = false;

          // This CharacterStyle set to BOLD FONT STYLE
          //rem app.changeTextPreferences.appliedCharacterStyle = myCharacterStyle;  

          app.findTextPreferences.findWhat = "Antioch";
          app.changeTextPreferences.changeTo = "Antioch";
          // rem myDocument.changeText();

          var foundItems = myDocument.findText();

          var foundItem = foundItems[0];

          foundItem.applyCharacterStyle(myCharacterStyle, false);

          • 2. Re: Finding and Changing "first word character syle" in text
            cuneytoral Level 1

            Thanks for your answer, it was very helpful.

            • 3. Re: Finding and Changing "first word character syle" in text
              Laubender Adobe Community Professional & MVP

              @cuneytoral – the explanation is:

               

              findText() ( or findGrep() ) are methods that return an array of found texts (formatted text objects).

              So the result with index 0 is the first one found. The last one is the length of the array -1.

               

              Since the result is of type Array and not Collection, we cannot define a range with the itemByRange() method.

              And also we cannot define the last item as allFoundItems[-1], but to have write this:

               

              allFoundItems[allFoundItems.length-1].appliedCharacterStyle = myCharacterStyle;
              

               

              to address the last found one.

               

              Say, we want to format the found texts from the second one found to the last one found, we have to loop through the found array:

               

              We do not start the loop with 0, but with 1:

               

              for(var n=1;n<allFoundItems.length;n++){
                  allFoundItems[n].appliedCharacterStyle = myCharacterStyle;
                  };
              

               

              Formatting every other of the found texts would be:

               

              for(var n=0;n<allFoundItems.length;n=n+2){
                  allFoundItems[n].appliedCharacterStyle = myCharacterStyle;
                  };
              

               

              Just to give you some ideas…

               

              Uwe

              • 4. Re: Finding and Changing "first word character syle" in text
                Laubender Adobe Community Professional & MVP

                Of course, if you want to format all found texts but the first one, it's more efficient to first format ALL found ones and then reformat the first one found. Just to make that clear ;-)

                 

                Uwe