17 Replies Latest reply on Aug 5, 2016 2:04 AM by madarmat

    Can you sort indesign pages alphabetically?

    laizeck84

      I have a document with over 200 page and I want to sort the pages alpahetically after the heading on every page.The headings have one paragrah style. The document is singel pages, but there is two text frames on ever page. Many pages have also the same heading, so I hope to sort them in the  same order in which the document is now.

       

      If you see the images under, I want to sort the pages alphabetically after the "heading" in bold to the right (txt:Best Buys Natural Wines)

      Is it possible with a script or something else?

      Skjermbilde+2012-10-11+kl.+12.03.18.png

        • 1. Re: Can you sort indesign pages alphabetically?
          Trevorׅ Adobe Community Professional

          Do the pages contain linked stories?

          If not you can script it easily with the move command

          • 2. Re: Can you sort indesign pages alphabetically?
            Trevorׅ Adobe Community Professional

            Ok This is what you were after.

            This assumes that you use a Paragraph style for your header AND THERE IS ONLY ONE HEADER ON EACH PAGE WHICH IS ONLY ONE PARAGRAPH LONG

             


            // Script by Trevor to sort Pages according to alphabetical order of Page Headers
            // http://forums.adobe.com/thread/1082984?tstart=0
            var doc =  app.documents[0], myHeaders, l;
            app.changeGrepPreferences = app.findGrepPreferences = null;
            app.findGrepPreferences.appliedParagraphStyle = "My Header"; 
            // Change above to the name of header style
            myHeaders = doc.findGrep();
            for (n=0; n < myHeaders.length-1; n++) $.writeln([myHeaders[n].contents]);
            myHeaders.sort (function (a,b) {if (a.contents.toLowerCase() > b.contents.toLowerCase())  return 1 else return -1})
            l = myHeaders.length;
            while (l--) myHeaders[l].parentTextFrames[0].parentPage.move (LocationOptions.AT_BEGINNING);
            
            • 3. Re: Can you sort indesign pages alphabetically?
              Trevorׅ Adobe Community Professional

              It also asumes that every page has a header

              • 4. Re: Can you sort indesign pages alphabetically?
                Trevorׅ Adobe Community Professional

                I thought I made I nice little script here that answered your question.

                 

                With all those bottles of wine I expected at least a "Cheers mate"

                 

                Any reason why you didn't mark the question as correct?

                • 5. Re: Can you sort indesign pages alphabetically?
                  mohamed a.h65064712

                  thanks ..awsome script i've been searching for a while

                   

                  but i need to add certain lines to this script hope you can help me

                   

                  now i have these paragraph style (for certain chapters)

                   

                  -Palm Trees ---paragraph style for chapter 1

                  -Trees ---paragraph style for chapter 2

                  -Shrubs---paragraph style for chapter 3

                  -Ground Covers---paragraph style for chapter 4

                  -Climbers---paragraph style for chapter 4

                  -Succulents---paragraph style for chapter 4

                   

                   

                  i want to arrange the pages that have these paragraph styles alphabetically in that exact order (arranging the paragraph)...can you help me in this?

                   

                  be noted that each chapter has its own Master Page

                  • 6. Re: Can you sort indesign pages alphabetically?
                    mohamed a.h65064712 Level 1

                    i've tried my luck in editing your code

                    var doc =  app.documents[0], Palmtrees, Trees, Shrubs, GroundCovers, Climbers, Succulents, l;
                    app.changeGrepPreferences = app.findGrepPreferences = null;  
                    app.findGrepPreferences.appliedParagraphStyle = "Plant Name (Palm trees)";
                    app.findGrepPreferences.appliedParagraphStyle = "Plant Name (Trees)";
                    app.findGrepPreferences.appliedParagraphStyle = "Plant Name (Shrubs)";
                    app.findGrepPreferences.appliedParagraphStyle = "Plant Name (GroundCovers)";
                    app.findGrepPreferences.appliedParagraphStyle = "Plant Name (Climbers)";
                    app.findGrepPreferences.appliedParagraphStyle = "Plant Name (Succulents)";
                    // Change above to the name of  Paragraph Styles   
                    Succulents = doc.findGrep();  
                    for (n=0; n < Succulents.length-1; n++) $.writeln([Succulents[n].contents]);  
                    Succulents.sort (function (a,b) {if (a.contents.toLowerCase() > b.contents.toLowerCase())  return 1 else return -1})  
                    l = Succulents.length;  
                    while (l--) Succulents[l].parentTextFrames[0].parentPage.move (LocationOptions.AT_BEGINNING);
                    Climbers = doc.findGrep();  
                    
                    
                    for (n=0; n < Climbers.length-2; n++) $.writeln([Climbers[n].contents]);  
                    Climbers.sort (function (a,b) {if (a.contents.toLowerCase() > b.contents.toLowerCase())  return 2 else return -2})  
                    l = Climbers.length;  
                    while (l--) Climbers[l].parentTextFrames[0].parentPage.move (LocationOptions.AT_BEGINNING);
                    
                    
                    for (n=0; n < GroundCovers.length-3; n++) $.writeln([GroundCovers[n].contents]);  
                    GroundCovers.sort (function (a,b) {if (a.contents.toLowerCase() > b.contents.toLowerCase())  return 3 else return -3})  
                    l = Climbers.length;  
                    while (l--) GroundCovers[l].parentTextFrames[0].parentPage.move (LocationOptions.AT_BEGINNING);
                    
                    
                    
                    
                    for (n=0; n < Shrubs.length-4; n++) $.writeln([Shrubs[n].contents]);  
                    Shrubs.sort (function (a,b) {if (a.contents.toLowerCase() > b.contents.toLowerCase())  return 4 else return -4})  
                    l = Shrubs.length;  
                    while (l--) Shrubs[l].parentTextFrames[0].parentPage.move (LocationOptions.AT_BEGINNING);
                    
                    
                    for (n=0; n < Trees.length-5; n++) $.writeln([Trees[n].contents]);  
                    Trees.sort (function (a,b) {if (a.contents.toLowerCase() > b.contents.toLowerCase())  return 5 else return -5})  
                    l = Trees.length;  
                    while (l--) Trees[l].parentTextFrames[0].parentPage.move (LocationOptions.AT_BEGINNING);
                    
                    
                    for (n=0; n < Palmtrees.length-6; n++) $.writeln([Palmtrees[n].contents]);  
                    Palmtrees.sort (function (a,b) {if (a.contents.toLowerCase() > b.contents.toLowerCase())  return 6 else return -6})  
                    l = PalmTrees.length;  
                    while (l--) Palmtrees[l].parentTextFrames[0].parentPage.move (LocationOptions.AT_BEGINNING);
                    alert("Finished!");
                    

                     

                    but sadly i get this error ...

                     

                    error..png

                     

                    can you please help me?

                    • 7. Re: Can you sort indesign pages alphabetically?
                      Trevorׅ Adobe Community Professional

                      Hi

                       

                      I'm too busy just now, to look into it either-way I don't understand what you are after.

                      If you split the document into chapters would that not help?

                       

                      Trevor

                      • 8. Re: Can you sort indesign pages alphabetically?
                        mohamed a.h65064712 Level 1

                        yes it would help the thing that is iam new to indesign scripting ...and i was just trying my luck

                        • 9. Re: Can you sort indesign pages alphabetically?
                          biozaichenko Level 1

                          The error message tells you what the error is - GroundCovers is not defined. It is declared but you are not assigning it any value.

                           

                          Your basic code would be:

                           

                          var doc =  app.documents[0], Palmtrees, Trees, Shrubs, GroundCovers, Climbers, Succulents, l;

                          app.changeGrepPreferences = app.findGrepPreferences = null;


                          app.findGrepPreferences.appliedParagraphStyle = "Plant Name (Succulents)";

                          Succulents = doc.findGrep();

                          for (n=0; n < Succulents.length-1; n++) $.writeln([Succulents[n].contents]);   

                          Succulents.sort (function (a,b) {if (a.contents.toLowerCase() > b.contents.toLowerCase())  return 1 else return -1})   

                          l = Succulents.length;  

                          while (l--) Succulents[l].parentTextFrames[0].parentPage.move (LocationOptions.AT_BEGINNING);


                          // then you repeat:


                          app.findGrepPreferences.appliedParagraphStyle = "Plant Name (Climbers)";

                          Climbers = doc.findGrep();

                          for (n=0; n < Climbers.length-1; n++) $.writeln([Climbers[n].contents]);   

                          Climbers.sort (function (a,b) {if (a.contents.toLowerCase() > b.contents.toLowerCase())  return 1 else return -1})   

                          l = Climbers.length;  

                          while (l--) Climbers[l].parentTextFrames[0].parentPage.move (LocationOptions.AT_BEGINNING);


                          etc... Basically you need to change what you are searching for BEFORE every search.


                          Let me know if this helps.


                          Michael

                          • 10. Re: Can you sort indesign pages alphabetically?
                            mohamed a.h65064712 Level 1

                            thanks so much Michael, worked perfectly




                             

                            • 11. Re: Can you sort indesign pages alphabetically?
                              madarmat

                              Hello,

                               

                              I found this script and it helps me alot, but i would like, if it's posible, to ignore numbers or the first 4 characters.

                              My problem is that my paragraph contains "year + name", and i want the pages sorted just by name. Now it sorts everything chronologicaly not alphabeticaly.

                              • 12. Re: Can you sort indesign pages alphabetically?
                                Trevorׅ Adobe Community Professional

                                Hi Mad

                                 

                                Not tested but replace

                                myHeaders.sort (function (a,b) {if (a.contents.toLowerCase() > b.contents.toLowerCase())  return 1 else return -1})

                                with

                                myHeaders.sort (function (a,b) {if (a.contents.toLowerCase().replace(/..../,"")  > b.contents.toLowerCase()).replace(/..../,"")
                                  return 1 else return -1});

                                 

                                HTH

                                 

                                Trevor

                                • 13. Re: Can you sort indesign pages alphabetically?
                                  biozaichenko Level 1

                                  I would replace like this:

                                   

                                  myHeaders.sort (function (a,b) {if (a.contents.toLowerCase().replace(/^\d{4}/,"")  > .contents.toLowerCase()).replace(/^\d{4}/,"")
                                    return 1 else return -1});

                                   

                                  //if the year is at the beginning of the paragraph but watch for trailing spaces

                                   

                                  or

                                   

                                  myHeaders.sort (function (a,b) {if (a.contents.toLowerCase().replace(/\d{4}/,"")  > .contents.toLowerCase()).replace(/\d{4}/,"")
                                    return 1 else return -1});

                                   

                                  //if year is not at the beginning

                                   

                                  Michael

                                  • 14. Re: Can you sort indesign pages alphabetically?
                                    Trevorׅ Adobe Community Professional

                                    Yes, i was being very lazy. But as long as the paragraphs are setup as described I don't think one would see any difference.

                                    • 15. Re: Can you sort indesign pages alphabetically?
                                      madarmat Level 1

                                      Thank you both for your interest and help, but i get an error for both your lines.

                                      ". does not have a value"

                                       

                                      The year is at the begining. If it was at the end the first script would have worked

                                      • 16. Re: Can you sort indesign pages alphabetically?
                                        Trevorׅ Adobe Community Professional
                                        myHeaders.sort(function(a, b) {
                                            if (a.contents.toLowerCase().replace(/\d{4}/, "") > b.contents.toLowerCase().replace(/\d{4}/, "")) return 1;
                                            else return -1;
                                        });
                                        

                                        Sorry there where typos in both our examples. The above should work.

                                        1 person found this helpful
                                        • 17. Re: Can you sort indesign pages alphabetically?
                                          madarmat Level 1

                                          Thank you very much. It worked perfectly!