6 Replies Latest reply on Mar 18, 2016 6:22 AM by Obi-wan Kenobi

    Choose one paragraph style and apply 1(2) above and 1(2) bellow new paragraph styles...finished script...

    kajzica Level 1

      If someone need to apply paragraph styles before and after main paragraph here is the script.

      1. Select text

      2. Run script

      3. Choose main paragraph around which you want to apply new styles

       

      Tnx to Goran G...

       

      Untitled-1.png

       

      // NextAndBeforeParagraphStyle

      // Goran Grčić 2016

       

       

       

       

       

       

      // Imput Dialog

      var imputPragraphDialog = function () {

              var docStyles=myDoc.paragraphStyles

              var myDialog = app.dialogs.add({name:"Dialog"});

              with (myDialog.dialogColumns.add()){

                  with(borderPanels.add()){

                      with(dialogColumns.add()){

                          staticTexts.add({staticLabel:"2. Paragraph above main:"});

                          staticTexts.add({staticLabel:"1. Paragraph above main:"});

                          staticTexts.add({staticLabel:"Main paragraph:"});

                          staticTexts.add({staticLabel:"1. Paragraph bellow main:"});

                          staticTexts.add({staticLabel:"2. Paragraph bellow main:"}); 

                          }

                      with(dialogColumns.add()){

                          var para2Above = dropdowns.add({stringList:docStyles.everyItem().name});

                          var para1Above = dropdowns.add({stringList:docStyles.everyItem().name});

                          var paraTarget = dropdowns.add({stringList:docStyles.everyItem().name});

                          var para1Below = dropdowns.add({stringList:docStyles.everyItem().name});

                          var para2Below = dropdowns.add({stringList:docStyles.everyItem().name});   

                          }

                      }

                  }

              // if OK

              if (myDialog.show() == true) {

                  // validate if Target Paragraph is selected

                  if (paraTarget.selectedIndex==-1) throw 'Please, choose main paragraph'

                  var myConfig = {

                          para2Above: para2Above.selectedIndex,

                          para1Above: para1Above.selectedIndex,

                          paraTarget: paraTarget.selectedIndex,

                          para1Below: para1Below.selectedIndex,

                          para2Below: para2Below.selectedIndex

                      }

                  myDialog.destroy();

                  return myConfig;

                  }

              // if Cancel

              else return   {

                          para2Above: -1,

                          para1Above: -1,

                          paraTarget: -1,

                          para1Below: -1,

                          para2Below: -1

                  }

      }

       

       

      // validate if TextFrame or Text is selected

      var validateSelection= function () {

          if (myDoc.selection.length==0)  throw 'Select some text'

          if ((myDoc.selection[0].parent.constructor.name!='Story') || (myDoc.selection[0].constructor.name=='InsertionPoint'))  throw 'Select some text'

          }

       

       

      // create progressbar

      var myCreateProgressPanel = function (myMaximumValue, myProgressBarWidth){

          progressPanel = new Window('window', 'Progress');

          progressPanel.add ('progressbar', [12, 12, myProgressBarWidth, 24], 0, myMaximumValue);

          return progressPanel

          }

        

       

       

      // main functon for finding Target Paragraph i style Paragraphs

      var findAndStyleParagraphs = function (myConfig) {

        

          var checkParaStyle = function (count) {

              switch (myParagraphs[count].appliedParagraphStyle.name) {

                  case myDoc.paragraphStyles[myConfig.paraTarget].name:

                     return false

                     break;

                  default:

                     return true

              }

          }

         

           // ----------------------------  FUNCTION PROGRAM ----------------------------------//

         

           var lenPara=myParagraphs.length;

           //create and shoe progressbar

           var myProgressPanel=myCreateProgressPanel(lenPara-1, 300)

           myProgressPanel.show()

           for (var i=0; i<lenPara; i++) {

               myProgressPanel.children[0].value = i

              if (myParagraphs[i].appliedParagraphStyle.name==myDoc.paragraphStyles[myConfig.paraTarget].n ame) {

                  if ((myConfig.para2Above!=-1)&&(i>1)&&(checkParaStyle(i-2))) myParagraphs[i-2].appliedParagraphStyle=myDoc.paragraphStyles[myConfig.para2Above];

                  if ((myConfig.para1Above!=-1) && (i>0)&&(checkParaStyle(i-1))) myParagraphs[i-1].appliedParagraphStyle=myDoc.paragraphStyles[myConfig.para1Above];

                  if ((myConfig.para1Below!=-1) && (i<lenPara-2)&&(checkParaStyle(i+1))) myParagraphs[i+1].appliedParagraphStyle=myDoc.paragraphStyles[myConfig.para1Below];

                  if ((myConfig.para2Below!=-1) && (i<lenPara-2)&&(checkParaStyle(i+2))) myParagraphs[i+2].appliedParagraphStyle=myDoc.paragraphStyles[myConfig.para2Below];

              }

          }

      }

       

       

       

       

      // ----------------------------   MAIN PROGRAM ----------------------//

       

       

      try {

          var myDoc=app.activeDocument;

          validateSelection();

          var myConfig=imputPragraphDialog();

       

       

           var myParagraphs=myDoc.selection[0].paragraphs;

           findAndStyleParagraphs (myConfig);

       

         } catch (err) {

            alert (err)

      }