4 Replies Latest reply on May 22, 2018 1:15 AM by suresh0208

    Find different text and apply different paragraph styles

    suresh0208

      Hi,

       

       

      I have so many documents. Which style may be repeated, But not next after next.

      My requirement is:

      Step 1:

      Find Text Called "SECTION" in content and apply the style name called "Level-1";

      Step 2:

      Find Numbers started in content like "1.1, 1.2, 1.3 to 16.1, 16.2, 16.3 so on " and apply the style name called "Level-2";

      Step 3: Remaining text Should apply "Para";

      Step 4: In Paragraph if Tab(Space) Comes before that apply the charecter style called "Para Bold".

       

      Below is a sample example:

      Find different text and apply different paragraph styles.jpg

       

      I am very new to scripting. can any one help !

       

       

      Below the code i have tried and getting a error:

       

       

      var myDoc = app.activeDocument,     

      myCodes = ["SECTION","Numbers"],   

      C = myCodes.length,  c;     

      app.findTextPreferences = null;   

      for ( c = 0; c < C; c++) {        

          app.findTextPreferences.findWhat = myCodes[c];        

          myFound = myDoc.findText();     

          var F = myFound.length,  f;

         

          var Style_01 = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Level-1");

          var Style_02 = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Level-2");

           

         

          for ( f = 0; f < F; f++) {   

              myFound[f].appliedCharacterStyle = myDoc.characterStyles[0];

             

              if ((myFound[f].appliedParagraphStyle == Style_01) ||

                  (myFound[f].appliedParagraphStyle == Style_02) ||

                  ) {   

                                  myFound[f].appliedParagraphStyle = MyStyle;

              } else{}

          }   

      }

       

       

      //I have tried this script too but not understanding,

      var main = function() {             

          //VARS           

          var doc = app.properties.activeDocument,           

          fgp = app.findGrepPreferences.properties,           

          texts = [], n = 0, nthText, myParaStyle,         

          texts2 = [], n = 0, nthText2, myParaStyle2,   

          texts3 = [], n = 0, nthText3, myParaStyle3,   

          texts4 = [], n = 0, nthText4, myParaStyle4,  

          paraStyleName = "Text New";    

          paraStyleName2 = "Text New 12";    

          paraStyleName3 = "Quotation New Bold";     

          paraStyleName4 = "Quotation New Italic"; 

                     

          //Exit if no documents open           

          if ( !doc ) {           

          alert("You need an open document" );           

          return;           

          }               

                     

          //Create ParaStyle if needed. Otherwise use it       

          var paraStyle = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName(paraStyleName);     

          myParaStyle = doc.paragraphStyles.item ( paraStyleName );           

          // if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );           

           

          var paraStyle2 = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName(paraStyleName2); 

          myParaStyle2 = doc.paragraphStyles.item ( paraStyleName2 );           

          // if ( !myParaStyle2.isValid ) myParaStyle2 = doc.paragraphStyles.add ( paraStyleName2 );   

       

       

          var paraStyle3 = doc.paragraphStyleGroups.itemByName("QUOTATION NEW").paragraphStyles.itemByName(paraStyleName3); 

          myParaStyle3 = doc.paragraphStyles.item ( paraStyleName3 );           

          // if ( !myParaStyle3.isValid ) myParaStyle3 = doc.paragraphStyles.add ( paraStyleName3 );   

       

       

          var paraStyle4 = doc.paragraphStyleGroups.itemByName("QUOTATION NEW").paragraphStyles.itemByName(paraStyleName4); 

          myParaStyle4 = doc.paragraphStyles.item ( paraStyleName4 );           

          // if ( !myParaStyle4.isValid ) myParaStyle4 = doc.paragraphStyles.add ( paraStyleName4 );  

                     

          //Setting grep find preferences           

          app.findGrepPreferences = null;         

          app.findGrepPreferences.properties = {           

          findWhat:".+",           

          }           

                     

          //Finding texts with static properties           

          texts = doc.findGrep();           

          n = texts.length;           

                 

          //Looping through found texts to inspect further properties with values ranges.           

          while ( n--  ) {nthText = texts[n];                    

              if (nthText.fontStyle =="Roman" && nthText.pointSize <= 10.5 ){nthText.appliedParagraphStyle = paraStyle;}  

              if (nthText.pointSize == 12){nthText.appliedParagraphStyle = paraStyle2;}  

              if (nthText.fontStyle == "Bold"){nthText.appliedParagraphStyle = paraStyle3;}  

              if (nthText.fontStyle == "Italic") {nthText.appliedParagraphStyle = paraStyle4;}    

          }       

                     

          //reverting UI values in the F/C panel           

          app.findGrepPreferences.properties = fgp;           

          }           

          var u;                  

          app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );

       

      Thanks in Advace

        • 1. Re: Find different text and apply different paragraph styles
          Loic.Aigon Adobe Community Professional

          myFound[f].appliedParagraphStyle = MyStyle;

           

          Where is "MyStyle" referenced ?

          And what kind of error do you get exactly ?

          • 2. Re: Find different text and apply different paragraph styles
            suresh0208 Level 1

            Hi Loic,

             

            I really don't have idea. I have searched some topics in forum and try to edit that.

             

            And the error i got is "Object Invalid".

             

            If you can help in this that would be great.

             

            Thanks

            • 3. Re: Find different text and apply different paragraph styles
              Loic.Aigon Adobe Community Professional

              And the error i got is "Object Invalid".

               

              there is probably more like the line number…

              • 4. Re: Find different text and apply different paragraph styles
                suresh0208 Level 1

                Hi Loic,

                 

                I am tried to edit a bit more and it is applying para style to text, But when i put a condition in text it's not working. When find text apply level-1 style. The updated codes as follows:

                 

                var myIndFile = File.openDialog ("Select Indesign File"); 

                var myWordFile = File.openDialog ("Select Word File"); 

                 

                app.open (myIndFile); 

                 

                var mydoc = app.activeDocument; 

                 

                mydoc.textPreferences.smartTextReflow = true; 

                mydoc.textPreferences.limitToMasterTextFrames = false; 

                mydoc.textPreferences.deleteEmptyPages = false; 

                mydoc.textPreferences.addPages = AddPageOptions.endOfStory; 

                 

                mydoc.pages.item(0).textFrames.item(0).place(File(myWordFile)); 

                 

                 

                /*Style AREA*/

                    try{

                    myParagraphStyle = mydoc.paragraphStyles.item("Para");

                    //If the paragraph style does not exist, trying to get its name will generate an error.

                    myName = myParagraphStyle.name;

                    }

                    catch (myError){

                    //The paragraph style did not exist, so create it.

                    myParagraphStyle = mydoc.paragraphStyles.add({name:"Para"});

                    }

                /*End of Style AREA*/

                    var myTextFrame = mydoc.pages.item(0).textFrames.item(0);

                    myTextFrame.parentStory.texts.item(0).applyParagraphStyle(myParagraphStyle, true);

                   

                   

                myCodes = ["SECTION"],  

                 

                 

                C = myCodes.length,  c;    

                 

                 

                app.findTextPreferences = null;  

                 

                 

                for ( c = 0; c < C; c++) {       

                 

                 

                    app.findTextPreferences.findWhat = myCodes[c];       

                 

                 

                    myFound = myDoc.findText();    

                 

                 

                    var F = myFound.length,  f;

                   

                     for ( f = 0; f < F; f++) {  

                          if ((myFound[f].appliedParagraphStyle == myParagraphStyle) && (myFound == myCodes)) {  

                 

                 

                                            myFound[f].appliedParagraphStyle = Level1;

                 

                 

                        }

                         }

                     }

                 

                 

                var wordName = myWordFile.name;

                 

                 

                app.activeDocument.save(new File(myIndFile.path + "/"  +myIndFile.name.replace(".indd", "") + "_" +wordName+"_Spanish" + "_V1.0.indd"));

                 

                Line no : 21 to 55 needs some suggestions to edit.

                 

                My next task is find line starts with numbers chage to level-2 style.

                 

                 

                Thanks,

                Suresh.