14 Replies Latest reply on Mar 20, 2013 5:35 AM by thomas.jeorge

    get Styles to new doc

    thomas.jeorge Level 1

      Hi All,

       

      I want to extract all the Heading1 and all the text applied specific character style (Month) related with its heading in orderwise in the new InDesign file.

       

      Is anyone can provide JS code.

       

      Like:

       

      This is Heading 1 appears first

      This is text using character style Month appear in first heading

      This is text using character style Month appear in first heading

      This is text using character style Month appear in first heading

      This is text using character style Month appear in first heading

       

      This is Heading 1 appears second

      This is text using character style Month appear in second heading

      This is text using character style Month appear in second heading

      This is text using character style Month appear in second heading

      This is text using character style Month appear in second heading

       

       

      This is Heading 1 appears third

      This is text using character style Month appear in third heading

      This is text using character style Month appear in third heading

      This is text using character style Month appear in third heading

      This is text using character style Month appear in third heading

       

       

      Thanks,

      Thomas

        • 1. Re: get Styles to new doc
          bgfrhlp

          Hello Thomas,

           

          This script creates a new document and imports the styles from the document, that is active when you start the script. Nothing fancy though.

           

          #target indesign

           

          doc = app.activeDocument;

          doc.save();

          pathName = doc.fullName;

          docNew = app.documents.add();

          docNew.importStyles(ImportFormat.CHARACTER_STYLES_FORMAT, pathName);

          • 2. Re: get Styles to new doc
            thomas.jeorge Level 1

            Thanks for your help.

             

            But, I want to extract all the text applied with  Heading 1 para style  and all the text applied with Month character style to new doc in its order.

             

            Like:

             

            Heading 1 then its following character style (Month) text. Again, next Heading 1 then its following character style (Month) text.....

             

            Thanks,

            Thomas

            • 3. Re: get Styles to new doc
              bgfrhlp Level 1

              There are several ways to do this. Good ol' copy paste works.

              You need to select the text you want to extract.

               

              #target indesign

               

              doc = app.activeDocument;

              if (app.selection.length == 1){

                  if (app.selection[0].constructor.name == "Text"){

                      app.copy();

                      newDoc = app.documents.add();

                      app.paste();

                  }

              }

              else {

                  alert("No text was selected");

              }

              • 4. Re: get Styles to new doc
                thomas.jeorge Level 1

                Sorry, dear. This is not what I want.

                 

                You have not got my question. I want only the text applied with heading 1(Para style) and Month(character style) style.

                 

                Please read again and help.

                 

                Thanks,

                Thomas

                • 5. Re: get Styles to new doc
                  Jump_Over Level 5

                  Hi,

                   

                  Here is some code using findText() method:

                  myDoc = app.activeDocument;
                  mPara = myDoc.paragraphStyles.item("Heading1"); // edit if needed
                  mChar = myDoc.characterStyles.item("Month"); // edit if needed
                  mCharsArr = new Array();
                  mString = "";
                  
                  mParasArr  = mFindText (myDoc, true);
                  for (k=0; k<mParasArr.length; k++) {
                       mCharsArr.push(mFindText (mParasArr[k], false) );
                       mString += mParasArr[k].contents.slice(0,-1) + "\n";
                       //alert (mString);
                       for (i=0; i<mCharsArr[k].length; i++)
                            mString += mCharsArr[k][i].contents + "\n";
                      mString = mString.slice(0,-1) + "\r";
                       }
                  myDoc1 =  app.documents.add();
                  myDoc1.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
                  myDoc1.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
                  MF = myDoc1.textFrames.add({geometricBounds: [72,72,100,432]});
                  MF.contents = mString;
                  MF.parentStory.paragraphs.everyItem().spaceBefore = 12;
                  MF.fit(FitOptions.FRAME_TO_CONTENT);
                  
                  app.findTextPreferences = null;
                  
                  function mFindText (source, pref) {
                  app.findTextPreferences = null;
                  if (pref) app.findTextPreferences.appliedParagraphStyle = mPara;
                  else app.findTextPreferences.appliedCharacterStyle = mChar;
                  return source.findText();
                  }
                  

                  enjoy,

                  Jarek

                  1 person found this helpful
                  • 6. Re: get Styles to new doc
                    thomas.jeorge Level 1

                    Thanks, Jarek.

                     

                    Its moving parastyles only. I also want character styles after Heading 1.

                     

                    Thanks,

                    Thomas

                    • 7. Re: get Styles to new doc
                      Jump_Over Level 5

                      Hi,

                       

                      If no charStyles text is placed - it means no charStyles was found in specific paraStyle text range.

                       

                      Or here is a misunderstanding:

                      "...all the text applied specific character style (Month) related with its heading in orderwise..."

                       

                      Explain what you mean "related", cause till now script is looking for charStyle occurrences within paraStyle text ranges.

                       

                      rgds

                      Jarek

                      • 8. Re: get Styles to new doc
                        thomas.jeorge Level 1

                        Hi Jarek,

                         

                        There are text applied with character style (Month) in the document.

                         

                        Yes, I want all the character style related with its heading in orderwise.

                         

                        Thanks,

                        Thomas

                        • 9. Re: get Styles to new doc
                          Jump_Over Level 5

                          Hi Thomas,

                           

                          The question is about the rule:

                           

                          - is 'Month' charStyle occurrence happens ONLY inside text with 'Heading1' paraStyle applied

                          or

                          - is 'Month' charStyle occurrence happens BETWEEN those paragraphs, so it could be possible that another paraStyle is applied to these texts.

                           

                          Jarek

                          • 10. Re: get Styles to new doc
                            thomas.jeorge Level 1

                            Hi Jarek,

                             

                            Yes, Month' charStyle occurrence happens BETWEEN those paragraphs, another paraStyle is applied to these texts.

                             

                            It is working, if Month charStyle applied on Heading1 text. But I also want other paragraphs text applied different parastyle and with Month charStyle.

                             

                            See below screenshot for clarification.

                             

                            output.png

                             

                             

                            Thanks,

                            Thomas

                            • 11. Re: get Styles to new doc
                              Jump_Over Level 5

                              Hi Thomas,

                               

                              I modified it a little.

                              It assumes all text you are looking for is inside the same story.

                               

                              var myDoc = app.activeDocument;
                              mPara = myDoc.paragraphStyles.item("Heading1"); // edit if needed
                              mChar = myDoc.characterStyles.item("Month"); // edit if needed
                              var mCharsArr = new Array();
                              var myDoc1, MF, k, i, mEnd, mStart, mTxtRange;
                              var mString = currStr = "";
                              mParasArr  = mFindText (myDoc, true);
                              for (k=0; k<mParasArr.length; k++) {
                                  mStart = mParasArr[k].index;
                                  if (k == mParasArr.length-1) mEnd = mParasArr[k].parentStory.characters[-1].index;
                                  else mEnd = mParasArr[k+1].index - 1;
                              
                                  mTxtRange = mParasArr[k].parentStory.characters.itemByRange(mStart,mEnd).texts[0];
                                  currStr = mFindText (mTxtRange, false);
                                  mCharsArr[k] = currStr;
                                  mString += mParasArr[k].contents.slice(0,-1) + "\n";
                                   for (i=0; i<mCharsArr[k][0].length; i++)
                                        mString += mCharsArr[k][0][i].contents + "\n";
                                  mString = mString.slice(0,-1) + "\r";
                                   }
                              myDoc1 =  app.documents.add();
                              myDoc1.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
                              myDoc1.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
                              MF = myDoc1.textFrames.add({geometricBounds: [72,72,100,432]});
                              MF.contents = mString;
                              MF.parentStory.paragraphs.everyItem().spaceBefore = 12;
                              MF.fit(FitOptions.FRAME_TO_CONTENT);
                              
                              app.findTextPreferences = null;
                              app.findGrepPreferences = null;
                              app.changeGrepPreferences = null;
                              app.findGrepPreferences.findWhat = "(\\r)\\r+";
                              app.changeGrepPreferences.changeTo = "$1";
                              myDoc1.changeGrep();
                              app.findGrepPreferences = null;
                              app.changeGrepPreferences = null;
                              
                              function mFindText (source, pref) {
                                  var res = new Array();
                              app.findTextPreferences = null;
                              if (pref) app.findTextPreferences.appliedParagraphStyle = mPara;
                              else app.findTextPreferences.appliedCharacterStyle = mChar;
                              res = source.findText();
                              return res;
                              }
                              

                               

                              Jarek

                              • 12. Re: get Styles to new doc
                                thomas.jeorge Level 1

                                Dear Jarek,

                                 

                                Thanks a lot for all your support. You are really very very very helpful. God Bless You!

                                 

                                Can you explain me the use of the parameter (pref) of below line

                                 

                                if (pref) app.findTextPreferences.appliedParagraphStyle = mPara;

                                 

                                Also, can we get the page numbers of those entries, if possible.

                                 

                                Thanks,

                                Thomas

                                 

                                 

                                 

                                 

                                 

                                • 13. Re: get Styles to new doc
                                  Jump_Over Level 5

                                  Hi, Thomas

                                   

                                  To include  page number (names) replace this part:

                                   

                                  var pNum;
                                       for (i=0; i<mCharsArr[k][0].length; i++) {
                                            pNum = mCharsArr[k][0][i].parentTextFrames[0].parentPage.name;
                                            mString += mCharsArr[k][0][i].contents + "\t" + pNum  + "\n";
                                       }
                                  

                                   

                                  var pref is a switcher inside function, cause it is used many times and sometimes preferences supposed to be changed before calling findText() method.

                                  If pref == true this method is searching for texts with specific para style applied

                                  If pref == false this method is searching for texts with specific char style applied.

                                   

                                  enjoy

                                  Jarek

                                  1 person found this helpful
                                  • 14. Re: get Styles to new doc
                                    thomas.jeorge Level 1

                                    Thanks so much, Jarek.