20 Replies Latest reply on Mar 31, 2015 12:10 PM by Peter Kahrel

    find text by paragraph style and place in new text frame with width 50mm

    patrickbcm Level 1

      Hello

       

      I'am looking voor a script dat find paragraph styles and place them in a new text frame with width 50mm.

      Is there a solution for?

       

      Kind Regards

      Patrick

        • 1. Re: find text by paragraph style and place in new text frame with width 50mm
          tanujad54435002 Level 1

          Hi,

           

          You can try following JS code:

           

          var txtFrame = app.activeDocument.textFrames

          var paragraphName = [];

          for(var i = 0; i< txtFrame.length; i++)

              {

                  if("" != txtFrame[i].contents)

                  {

                  var ttlParagraph = txtFrame[i].paragraphs // traverse each textframe one by one and fetching it's paragraphs

                  for(var j=0; j< ttlParagraph.length; j++) // traversing each paragraph one by one

                      {

                          paragraphName.push(ttlParagraph[j].appliedParagraphStyle.name)

                          alert(paragraphName[j])

                         

                      }

                  }

              }

          // Adding a new textframe to insert all paragraph style names found

          var newTxtFrame = app.activeDocument.textFrames.add()

          //Saving original units of document so that once task get completed, it can change to its original value

          var origHorizontalUnit = document.viewPreferences.horizontalMeasurementUnits;

          var origVerticalUnit = document.viewPreferences.verticalMeasurementUnits;

                

          //Changing to mm

          document.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;

          document.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

             

          newTxtFrame.geometricBounds = new Array(50,50,100,100) // you can change geometric bounds as per your document objects. I have taken this to show just an example

          newTxtFrame.contents = ""

          var nm

          for(var i = 0; i< paragraphName.length; i++)

              {

                  newTxtFrame.contents = newTxtFrame.contents +  paragraphName[i] + "\n"

              }

          newTxtFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY // increasing textframe height in case of overset

           

           

          //restoring the original units

          document.viewPreferences.horizontalMeasurementUnits =origHorizontalUnit;

          document.viewPreferences.verticalMeasurementUnits = origVerticalUnit;

           

           

          Hope this would help to resolve your issue.

          • 2. Re: find text by paragraph style and place in new text frame with width 50mm
            patrickbcm Level 1

            I put this in line 2

            var paragraphName = [tabelrij];

             

            Then i get a error see attachmentSchermafbeelding 2015-03-26 om 13.59.40.png

            • 3. Re: find text by paragraph style and place in new text frame with width 50mm
              tanujad54435002 Level 1

              Hi,

               

              I have done few changes on script, you can try this one. How ever the old one is also working at my end. If the same issue exist then could you please share a video how are you using this script.

               

              var txtFrame = app.activeDocument.textFrames

              var paragraphName = new Array();

              for(var i = 0; i< txtFrame.length; i++)

                  {

                      if("" != txtFrame[i].contents)

                      {

                      var ttlParagraph = txtFrame[i].paragraphs // traverse each textframe one by one and fetching it's paragraphs

                      for(var j=0; j< ttlParagraph.length; j++) // traversing each paragraph one by one

                          {

                              paragraphName.push(ttlParagraph[j].appliedParagraphStyle.name) // inserting the paragraph style name into array

                          }

                      }

                  }

              // Adding a new textframe to insert all paragraph style names found

              var newTxtFrame = app.activeDocument.textFrames.add()

              //Saving original units of document so that once task get completed, it can change to its original value

              var origHorizontalUnit = document.viewPreferences.horizontalMeasurementUnits;

              var origVerticalUnit = document.viewPreferences.verticalMeasurementUnits;

                    

              //Changing to mm

              document.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;

              document.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

                 

              newTxtFrame.geometricBounds = new Array(50,50,100,100) // you can change geometric bounds as per your document objects. I have taken this to show just an example

              newTxtFrame.contents = ""

              var nm

              for(var i = 0; i< paragraphName.length; i++)

                  {

                      newTxtFrame.contents = newTxtFrame.contents +  paragraphName[i] + "\n"

                  }

              newTxtFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY // increasing textframe height in case of overset

               

               

              //restoring the original units

              document.viewPreferences.horizontalMeasurementUnits =origHorizontalUnit;

              document.viewPreferences.verticalMeasurementUnits = origVerticalUnit;

              • 4. Re: find text by paragraph style and place in new text frame with width 50mm
                [Jongware] Most Valuable Participant

                The error is about this:

                 

                var paragraphName = [tabelrij];

                 

                -- it is not mentioned in the answer, but you should put literal names between straight single or double quotes, e.g.

                 

                var paragraphName = [ "tabelrij" ];

                 

                or for multiple items

                 

                var paragraphName = [ "tabelrij", "intro", "balk" ];

                • 5. Re: find text by paragraph style and place in new text frame with width 50mm
                  patrickbcm Level 1

                  Hello this is working but i think i wrote my answer wrong.

                  I must have the content of the paragraph style and not the name of a paragraph style in a text frame.

                  And the place were he find a paragraph style he must make a text frame with the content on the same place.

                  • 6. Re: find text by paragraph style and place in new text frame with width 50mm
                    tanujad54435002 Level 1

                    If your document do not have any textframe with contents then you can try this one:

                     

                    var totalParaStyle = app.activeDocument.allParagraphStyles

                    var paragraphName = new Array();

                    for(var i=0; i<totalParaStyle.length; i++)

                        {

                            paragraphName.push(totalParaStyle[i].name) // inserting the paragraph style name into array

                        }

                    // Adding a new textframe to insert all paragraph style names found

                    var newTxtFrame = app.activeDocument.textFrames.add()

                    //Saving original units of document so that once task get completed, it can change to its original value

                    var origHorizontalUnit = document.viewPreferences.horizontalMeasurementUnits;

                    var origVerticalUnit = document.viewPreferences.verticalMeasurementUnits;

                          

                    //Changing to mm

                    document.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;

                    document.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

                       

                    newTxtFrame.geometricBounds = new Array(50,50,100,100) // you can change geometric bounds as per your document objects. I have taken this to show just an example

                    newTxtFrame.contents = ""

                    var nm

                    for(var i = 0; i< paragraphName.length; i++)

                        {

                            newTxtFrame.contents = newTxtFrame.contents +  paragraphName[i] + "\n"

                        }

                    newTxtFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY // increasing textframe height in case of overset

                    //restoring the original units

                    document.viewPreferences.horizontalMeasurementUnits =origHorizontalUnit;

                    document.viewPreferences.verticalMeasurementUnits = origVerticalUnit;

                    • 7. Re: find text by paragraph style and place in new text frame with width 50mm
                      patrickbcm Level 1

                      Its not really what i want. See attachment what i mean.

                      Schermafbeelding 2015-03-26 om 16.45.39.png

                      • 8. Re: find text by paragraph style and place in new text frame with width 50mm
                        tanujad54435002 Level 1

                        Hi Patrick,

                         

                        From the attachment what i have understood you want a new textframe adjacent to every textframe which consist of information about the paragraph style used. I have updated the script. Try this.

                         

                        // function to check total textframes in the document and the paragraph style

                        function findTxtFrmStyle()

                        {

                            var txtFrame = app.activeDocument.textFrames.everyItem().getElements()

                           

                            for(var i = 0; i< txtFrame.length; i++)

                                addTxtFrame(txtFrame[i])   

                        }

                         

                         

                        // function to add new textframe adjacent to the textframe with contents to fetch it's paragraph style

                        function addTxtFrame(txtFrame)

                        {

                            var paragraphName =  new Array();

                            //Saving original units of document so that once task get completed, it can change to its original value

                            var origHorizontalUnit = document.viewPreferences.horizontalMeasurementUnits;

                            var origVerticalUnit = document.viewPreferences.verticalMeasurementUnits;

                                  

                            //Changing to mm

                            document.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;

                            document.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

                              //  var a = txtFrame.geometricBounds[3] + 5;

                             

                             if("" != txtFrame.contents)

                               {

                                 var ttlParagraph = txtFrame.paragraphs // traverse each textframe one by one and fetching it's paragraphs

                                  for(var j=0; j< ttlParagraph.length; j++) // traversing each paragraph one by one

                                      {

                                          paragraphName.push(ttlParagraph[j].appliedParagraphStyle.name) // inserting the paragraph style name into array

                                      }

                               }

                             else

                                    return;

                           

                            // Adding a new textframe to insert all paragraph style names found

                            var newTxtFrame = app.activeDocument.textFrames.add()       

                            newTxtFrame.geometricBounds = new Array(txtFrame.geometricBounds[0], txtFrame.geometricBounds[3] + 5,txtFrame.geometricBounds[2],txtFrame.geometricBounds[3] + 55) // you can change geometric bounds as per your document objects. I have taken this to show just an example

                            newTxtFrame.contents = ""

                            var nm

                            for(var i = 0; i< paragraphName.length; i++)

                                {

                                    newTxtFrame.contents = newTxtFrame.contents +  paragraphName[i] + "\n"

                                }

                            newTxtFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY // increasing textframe height in case of overset

                             //restoring the original units

                            document.viewPreferences.horizontalMeasurementUnits =origHorizontalUnit;

                            document.viewPreferences.verticalMeasurementUnits = origVerticalUnit;

                        }

                         

                         

                        findTxtFrmStyle();

                        • 9. Re: find text by paragraph style and place in new text frame with width 50mm
                          patrickbcm Level 1

                          No this is not what i mean.

                           

                          I have a text frame with different paragraph styles. Now the script have to look to the content with the paragraph style 'tabelrij'. The text with paragraph style tabelrij what he found must he place into a new text frame on the same place where he found the text with the paragraph style.

                           

                          I found a script but that makes a tabel.  See below, but i want a text frame into a text frame.

                           

                          function main() {

                                    app.findTextPreferences = app.changeTextPreferences = null;

                                    app.findTextPreferences.appliedParagraphStyle = "tabel rij" //won't work it it's in a style group

                                    var myF = app.activeDocument.selection[0].parentStory.findText(true); //make sure you are inside a text frame when running the script

                                    for (var i = 0; i < myF.length; i++) {

                                              var myT = myF[i].convertToTable();

                                              myT.appliedTableStyle = "spel";

                                              myT.clearTableStyleOverrides(); //this line might need to be commented out, see how it works with or without it

                                              myT.width=54;

                                    }

                                    alert("All done!\nYou can go outside and play now!");

                          }

                          app.doScript('main()', undefined, undefined, UndoModes.entireScript, 'AllToTables');

                          • 10. Re: find text by paragraph style and place in new text frame with width 50mm
                            Peter Kahrel Adobe Community Professional & MVP

                            Patrick,

                             

                            Your screenshot in post #7 suggests that you want a frame drawn around the bidding summary. If that is the case, it's not necessary to remove the text, place it in a frame and place the frame. Much easier simply to draw a border behind the text.

                             

                            Peter

                            • 11. Re: find text by paragraph style and place in new text frame with width 50mm
                              patrickbcm Level 1

                              Hello Peter

                               

                              What happens then with frame behind when i have text corrections. I think the content must be placed in a text frame.

                               

                              Patrick

                              • 12. Re: find text by paragraph style and place in new text frame with width 50mm
                                Peter Kahrel Adobe Community Professional & MVP

                                Ah, you never mentioned corrections. In that case the easiest solution is not a text frame but a single-cell table. First create a cell style and a table style (say, 'score'), the use this script to place all blocks of your tabelrij paragraphs in single-cell tables:

                                 

                                app.findGrepPreferences = null;
                                app.findGrepPreferences.appliedParagraphStyle = app.documents[0].paragraphStyles.item('tabelrij');
                                scores = app.documents[0].findGrep();
                                for (i = scores.length-1; i >= 0; i--) {
                                    table = scores[i].characters.itemByRange (0, scores[i].characters.length-2).convertToTable ('£', '@');
                                    table[0].width = '50mm';
                                    table[0].appliedTableStyle = app.documents[0].tableStyles.item('score');
                                }
                                
                                

                                 

                                Use the cell style to fine-tune border width, insets, etc.

                                 

                                Peter

                                • 13. Re: find text by paragraph style and place in new text frame with width 50mm
                                  patrickbcm Level 1

                                  Hello Peter

                                   

                                  It's working, but we use a system where we can check in/out indesign and incopy documents. That system has a little bug. The system do not accept this. It only works when it's a new text frame.

                                  Is that possible.

                                  • 14. Re: find text by paragraph style and place in new text frame with width 50mm
                                    Peter Kahrel Adobe Community Professional & MVP

                                    Aha. A little bug. You should have mentioned that earlier! Create an object style with the required settings (fixed column, width 50mm, Auto-Size from the top only, anchored object: above line), then do this script;

                                     

                                    app.findGrepPreferences = null;  
                                    app.findGrepPreferences.appliedParagraphStyle = app.documents[0].paragraphStyles.item('tabelrij');  
                                    scores = app.documents[0].findGrep();  
                                    for (i = scores.length-1; i >= 0; i--) {  
                                       frame = scores[i].insertionPoints[-1].textFrames.add();
                                       scores[i].characters.itemByRange (0, scores[i].characters.length-2).move (LocationOptions.after, frame.insertionPoints[0]);
                                       frame.applyObjectStyle (app.documents[0].objectStyles.item('score'));
                                    }
                                    
                                    • 16. Re: find text by paragraph style and place in new text frame with width 50mm
                                      Peter Kahrel Adobe Community Professional & MVP

                                      Works fine over here. What version inDesign do you use?

                                      • 17. Re: find text by paragraph style and place in new text frame with width 50mm
                                        patrickbcm Level 1

                                        I use Indesign CS6. I make a new document and its working.

                                        Do you know how to set de width to 50mm of the new text frame and the height to automatic?

                                        • 18. Re: find text by paragraph style and place in new text frame with width 50mm
                                          Peter Kahrel Adobe Community Professional & MVP

                                          Width: text frame general options, Colums: Fixed width, then 50mm at Width

                                          Height: text frame auto size options

                                           

                                          (Both these in the object styles dialog)

                                          • 19. Re: find text by paragraph style and place in new text frame with width 50mm
                                            patrickbcm Level 1

                                            Yes it's working.

                                            Is it also possible that i can find to a table style and put that in a new text frame?

                                            For example find table style 'score' and put it into a new text frame.

                                            • 20. Re: find text by paragraph style and place in new text frame with width 50mm
                                              Peter Kahrel Adobe Community Professional & MVP

                                              No, table styles are for tables, object styles are for text frames.