24 Replies Latest reply on Oct 22, 2016 8:49 AM by Obi-wan Kenobi

    Sort paragraphs in a certain way

    cmoke73 Level 1

      Hi there.

      My current thread deals with the re-sorting of the paragraphs in a text frame.

       

      This is like it looks before:

       

      HEADLINE

       

      First text (description). xxxxxxxxxxxxx

      xxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx xxxxxx

      xxxxxxxxxxxxx xxxxxxxxxxx xxxxxxxxx xxxxxxx

      xxxxxxxxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx xxx

      xxxxxxxxxxxxxxx xxxxxxxx xxxxxxx.

       

      Second descriptive text xxxxxx xxxxxxxx x

      xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxx xxx

      xxxxxxxxxxxxx xxxxxxxxx xxxxxxxxxxxxxx xxxxxxxx

       

      Article 1

      Article 2

      Article 3

      Article n

       

      This is how I want to have it after re-sorting:

       

      HEADLINE

       

      First text (description). xxxxxxxxxxxxx

      xxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx xxxxxx

      xxxxxxxxxxxxx xxxxxxxxxxx xxxxxxxxx xxxxxxx

      xxxxxxxxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx xxx

      xxxxxxxxxxxxxxx xxxxxxxx xxxxxxx.

       

       

       

      Article 1

      Article 2

      Article 3

      Article n

       

      Second descriptive text xxxxxx xxxxxxxx x

      xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxx xxx

      xxxxxxxxxxxxx xxxxxxxxx xxxxxxxxxxxxxx xxxxxxxx

       

      The number of articles changes. Each of them is a paragraph for itself.

      Next step will be the new formating of the paragraphs,  if it is important for this thread...

      I guess it works with an array in which all paragraphs are stored. Then they must be called up in a special order.

      Only: I do not know the correct syntax for that... Can anybody help me in the easiest way?

       

       

       

       

       

        • 1. Re: Sort paragraphs in a certain way
          Skemicle Level 3

          Have you tried to use the move() function to set the location options at end?

          • 2. Re: Sort paragraphs in a certain way
            cmoke73 Level 1

            No, I do not know that function or how to use it, yet.

            • 3. Re: Sort paragraphs in a certain way
              Skemicle Level 3

              You can get some information on it at: Adobe InDesign CS5 (7.0) Object Model JS: Paragraph. If the second descriptive text paragraph is always the third paragraph, behind the Headline and first descriptive text; you can reference it as app.selection[0].paragraphs[2]

              • 4. Re: Sort paragraphs in a certain way
                Obi-wan Kenobi Adobe Community Professional

                Skemicle is right! 

                 

                Something like this:

                 

                Before:

                Capture d’écran 2016-10-20 à 22.22.54.png

                 

                After:

                 

                Capture d’écran 2016-10-20 à 22.23.22.png

                 

                app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, 'Red/Green"');
                  
                function main()
                {  
                
                
                var myDoc = app.activeDocument;
                app.findGrepPreferences = app.changeGrepPreferences = null;  
                app.findGrepPreferences.findWhat = "(?s).+";
                app.findGrepPreferences.appliedParagraphStyle = "Red";
                myFound_red = myDoc.findGrep( );
                
                
                app.findGrepPreferences = app.changeGrepPreferences = null;  
                app.findGrepPreferences.findWhat = "(?s).+";
                app.findGrepPreferences.appliedParagraphStyle = "Green";
                myFound_Green = myDoc.findGrep( );
                
                
                for (i = 0; i < myFound_red.length; i++) {    
                myRed = myFound_red[i]
                myGreen = myFound_Green[i];
                myRed.move (LocationOptions.BEFORE, myGreen);  
                }
                
                
                app.findGrepPreferences = app.changeGrepPreferences = null;  
                
                
                }
                

                 

                 

                (^/)

                • 5. Re: Sort paragraphs in a certain way
                  Skemicle Level 3

                  Obi-wan,

                  I wasn't going to provide a working code until I saw an attempt made by cmoke. Though I wasn't going for paragraph styles

                  I was thinking more along the lines of:

                  1. adding "\r" as the last character of the textFrame,
                  2. moving paragraph[2] to the end of the textFrame,
                  3. removing the last character from the textFrame.

                  Yes, your script is better (if the paragraphs are formatted correctly with paragraph styles) and works all the time (multiple headlines in one textFrame), though i was just going to get cmoke started with something a bit simpler.

                  • 6. Re: Sort paragraphs in a certain way
                    cmoke73 Level 1

                    thank you Skemicle.

                    Headline stays on top. The second descriptive text must always become the last paragraph.

                    Between those "mass-texts" must be the articles.

                    • 7. Re: Sort paragraphs in a certain way
                      cmoke73 Level 1

                      Hi Obi-wan.

                      Give me a moment to understand your responce...

                       

                      Then I make my tries.

                      • 8. Re: Sort paragraphs in a certain way
                        Skemicle Level 3

                        quick question.. would each new headline be in the same textFrame or would it get a separate textFrame?

                        • 9. Re: Sort paragraphs in a certain way
                          cmoke73 Level 1

                          For each article there is a separate text frame.

                          Each of those text frames contains (the order I want to have it after):

                           

                          1. a headline

                          2. text #1

                          3. article number (color 1)

                          4. article number (color 2)

                          5. article number (color 3)

                          6. ...

                          x. text #2

                           

                          Instead of different colors it could be different material, but the same article. That´s the reason why you never know how many article numbers you have to re-sort.

                          • 10. Re: Sort paragraphs in a certain way
                            Obi-wan Kenobi Adobe Community Professional

                            As separate text frames, it could be quicker to simply move the third para after the last! 

                             

                            (^/)

                            • 11. Re: Sort paragraphs in a certain way
                              cmoke73 Level 1

                              I think so. the question ist: how?

                              I have to find a way to split them and move the last one at the end. But I don´t "speak" javascript that good.

                              • 12. Re: Sort paragraphs in a certain way
                                cmoke73 Level 1

                                I would begin like this (It does not really work, but there are also no error messages):

                                 

                                var doc = app.documents;  
                                var dieAuswahl = app.selection[0] ;
                                var ersterAbsatz = dieAuswahl.paragraphs[0];    
                                
                                var myParagraphs = app.selection[0].paragraphs;
                                var myArray = new Array(); 
                                myArray = myParagraphs.everyItem().contents;
                                myArray[2].appliedParagraphStyle = "Red"; //try to apply a paragraph style to the 3rd paragraph - nothing happens
                                
                                // an other way???
                                //~ for ( i=0; i<myParagraphs.length; i++) {
                                //~     myArray[i];
                                //~     }
                                  
                                
                                

                                 

                                but how can I pick out now the last para and move it?

                                • 13. Re: Sort paragraphs in a certain way
                                  Obi-wan Kenobi Adobe Community Professional

                                  This version seems to work but apparently a problem somewhere!!

                                   

                                  To make it work, we need to limit the treatment of the script on the good text frames, e.g., only the text frames with a specific para style!

                                  In my sample, the "Blue_Para" paras are the target!

                                   

                                  So … if we consider all text frames are independent frames (no threaded):

                                   

                                  Capture d’écran 2016-10-21 à 02.59.07.png

                                  Capture d’écran 2016-10-21 à 02.59.48.png

                                   

                                  … using this script, not perfect [to be validated]:

                                   

                                  var myDoc = app.documents.item(0);
                                  var myTFrames = myDoc.textFrames;
                                  
                                  
                                  for (t = 0 ; t < myTFrames.length; t++)
                                      {
                                          var myTFrame = myTFrames[t];
                                          var myParas = myTFrame.parentStory.paragraphs;  
                                          
                                          if (myParas[0].appliedParagraphStyle.name == "Blue_Para")
                                              {
                                                  app.findGrepPreferences = app.changeGrepPreferences = null;
                                                  app.findGrepPreferences.findWhat = ".\\z(?!\\r)";
                                                  app.changeGrepPreferences.changeTo = "$0\r";
                                                  myTFrame.changeGrep();
                                                  
                                                  var n = myTFrame.parentStory.paragraphs.length;
                                              
                                                  myParas[2].move(LocationOptions.AFTER, myParas[n-1]);
                                                  
                                                  app.findGrepPreferences = app.changeGrepPreferences = null;
                                                  app.findGrepPreferences.findWhat = "\\r\\z";
                                                  app.changeGrepPreferences.changeTo = "";
                                                  myTFrame.changeGrep();
                                              }
                                      }
                                  

                                   

                                   

                                  (^/)

                                  • 14. Re: Sort paragraphs in a certain way
                                    cmoke73 Level 1

                                    Cool, it works!

                                     

                                    It took a little to analyze your script. The precondition is to designate all the paragraphs with the style "red". So the headlne has to re-styled before you use that script. Isn´t it?

                                     

                                    thank you!

                                    • 15. Re: Sort paragraphs in a certain way
                                      Obi-wan Kenobi Adobe Community Professional

                                      No! Here: "Blue_Para" style!

                                       

                                      This's the Script target!

                                       

                                      From this beginning, what is interesting is that you can" style" all your text inside the script!

                                       

                                      (^/)

                                      • 16. Re: Sort paragraphs in a certain way
                                        Skemicle Level 3

                                        Your original script attempt doesn't work only because you are applying the paragraph style to the contents rather than the paragraph.. apply the appliedParagraph style to myParagraphs[2] rather than myArray[2].

                                        BUT... You don't need to do anything with styles for the script to do what you want.. Back to my comment to Obi-wan

                                        1. adding "\r" as the last character of the textFrame,
                                        2. moving paragraph[2] to the end of the textFrame,
                                        3. removing the last character from the textFrame.

                                        That's all you need no styling necessary:

                                        sel = app.selection[0];
                                        sel.characters[-1].contents = "\r";
                                        sel.paragraphs[2].move(LocationOptions.AT_END);
                                        sel.characters[-1].contents = "";
                                        

                                        I just needed to see you attempt for yourself before i would provide this .

                                        • 17. Re: Sort paragraphs in a certain way
                                          Obi-wan Kenobi Adobe Community Professional

                                          Skemicle,

                                           

                                          This code could be fine in some particular conditions!

                                           

                                          1/ Selection

                                          2/ if last para isn't a blank para

                                          3/ if no other text frame to be treated

                                           

                                          Not too realistic for my taste!

                                           

                                          (^/)

                                          • 18. Re: Sort paragraphs in a certain way
                                            cmoke73 Level 1

                                            Okay, this solution works better. And is short. Thank you for that!

                                            Can you please tell me what happens in row 2 and row 4? What does that [-1] do?

                                             

                                            "I just needed to see you attempt for yourself before i would provide this" ...and you was right!

                                            I WANT to understand it. But the time is now too short for reading books and digging deep in the materia. I need the solution until monday. The books I have I read occasionally...

                                            • 19. Re: Sort paragraphs in a certain way
                                              cmoke73 Level 1

                                              Hi Obi-wan!

                                              I´m sure, when I get better in scripting, I will utilise your helpful script much more. But, now, it is much too complex. I would have to adapt some things in a way I cannot do now.

                                               

                                              However I appreciate your help.

                                              • 20. Re: Sort paragraphs in a certain way
                                                Obi-wan Kenobi Adobe Community Professional

                                                No annoyance for me! 

                                                 

                                                More important and more annoying! 

                                                After simplifying my script according to Skemicle's excellent writing, I'm always feeling that something runs wrong [line 09 with the "if"]

                                                 

                                                 

                                                var myDoc = app.documents.item(0);
                                                var myTFrames = myDoc.textFrames;
                                                
                                                
                                                for ( t = 0 ; t < myTFrames.length; t++ )
                                                    {
                                                        var myTFrame = myTFrames[t];
                                                        
                                                        if  (  myTFrame.paragraphs[0].appliedParagraphStyle.name == "Blue_Para"  )
                                                            {
                                                                myTFrame.characters[-1].contents = "\r";  
                                                                myTFrame.paragraphs[2].move(LocationOptions.AT_END);  
                                                                myTFrame.characters[-1].contents = "";  
                                                            }
                                                    }
                                                

                                                 

                                                 

                                                The difference between Skemicle's approach and mine is that I treat all the doc, not only a selection.

                                                That's why I've tried to find a target ("Blue_Para" para style applied to the first para)!

                                                 

                                                (^/) 

                                                • 21. Re: Sort paragraphs in a certain way
                                                  Skemicle Level 3

                                                  line 2 should be changed to "sel.insertionPoints[-1].contents = "\r";" I realized when i went to explain that I messed up.

                                                  but [-1] says "The last item in the array" So then lines 2 and four say Set the last character in the text frame to "\r"(paragraph return) and "" (delete character) respectively.

                                                  • 22. Re: Sort paragraphs in a certain way
                                                    cmoke73 Level 1

                                                    This ist how I understood the four lines in translation:

                                                    1. create a variable and fill it with the information about the selected item

                                                    2. the place you want to insert something should allways be the last character (a paragraph return)

                                                    3. pick up (from your selected item) the paragraph at the third position and move it at the end of ... (what?)

                                                    4. delete the last character in my selected item (the TextFrame)

                                                     

                                                     

                                                    Is this tolerably correct?

                                                     

                                                    • 23. Re: Sort paragraphs in a certain way
                                                      Laubender Adobe Community Professional & MVP

                                                      Skemicle wrote:

                                                      … but [-1] says "The last item in the array"…

                                                      No. [-1] says the last item of a collection of items.

                                                      It's not an array. The last item of an array would be array[array.length-1].

                                                      [-1] notation would not work with arrays. The result would be "undefined".

                                                       

                                                      Regards,
                                                      Uwe