4 Replies Latest reply on Nov 16, 2012 4:13 AM by indegn5

    Auto apply paragraph style

    indegn5 Level 1

      Hi all,


      I m new to indesign scripting. I need a help to correct my script.

      Its a basic scritpt to created to apply paragraph style 1 to the basic paragraph . after doing this, i want to apply the paragraph style 2 created to the next paragraphs likewise and so on.


      var myDoc = app.activeDocument;

      app.findGrepPreferences = NothingEnum.NOTHING;

      app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyles.itemByName("[Basic Paragraph]");

      var myResult = app.findGrep();


      app.changeGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyles.itemByName("Paragraph Style 1");

      var myResult1 = app.changeGrep();

          app.findGrepPreferences = NothingEnum.NOTHING; //to clear the selected paragraph panel

          app.changeGrepPreferences = NothingEnum.NOTHING; //to clear the changed paragraph panel



      Can anybody help on this, please.



        • 1. Re: Auto apply paragraph style
          Vamitul Level 4

          what error does it give??


          try this


          var doc=app.activeDocument;
          function resetOpt(){ // saves the find change options and resets the to default
              var old_fCGOpts=app.findChangeGrepOptions.properties;
              var old_fGPref=app.findGrepPreferences.properties;
              var old_cGPref=app.changeGrepPreferences.properties;
              return [old_fCGOpts,old_fGPref,old_cGPref]
          function restoreOpt(myOpt){
          var myOpt=resetOpt(); //save and reset Find/Change Options
          app.findGrepPreferences.appliedParagraphStyle = doc.paragraphStyles.itemByName("[Basic Paragraph]");
          app.changeGrepPreferences.appliedParagraphStyle = doc.paragraphStyles.itemByName("Paragraph Style 1");
          var myResult1 = doc.changeGrep();
          restoreOpt(myOpt); //restore previous find change options

          • 2. Re: Auto apply paragraph style
            indegn5 Level 1

            your script a wow!


            thanks vami.



            • 3. Re: Auto apply paragraph style
              Jump_Over Level 5



              So, if myResult is an array with all textRanges with Basic Paragraph applied

              start to iterate it:


              var ParaStylesToAdd_ArrayOfNames = ["P4", "P3", "P2"];

              var counter = ParaStylesToAdd_ArrayOfNames.length;

              from (k=0; k<myResult.length; k++)


                   myStory = myResult[k].parentStory;


                   basePara = myResult[k].paragraphs[-1];

                   while (counter--)


                        basePara = myStory.paragraphs.nextItem(basePara);

                        basePara.appliedParagraphStyle = myDoc.paragraphStyles.itemByName(ParaStylesToAdd_ArrayOfNames[counter-1]);





              Watch two assumings here:

              1. there is enough paras in myStory starting from Basic Para ==> to add your next styles.

              2. if two or more Basic Paras are in the same MyStory ==> they can't be closer then ParaStylesToAdd_ArrayOfNames.length distance of paragraphs.

              Watch also up_side_down order of para names in an array;



              • 4. Re: Auto apply paragraph style
                indegn5 Level 1

                Thanks 30.png


                I'll try on yours.