10 Replies Latest reply on Jul 5, 2017 5:48 PM by sw52308871

    Sort spreads alphabetically INDD CS6 or CC

    sw52308871

      I have 4 INDD files (currently INDD CS6) that all contain 2-page spreads with per-country data on each spread. Each file covers a single world region with countries sorted alphabetically - so Sub-Saharan Africa has "Angola, Benin, Botswana ..." while Latin America and Caribbean has "Argentina, Barbados, Belize..." and South/East Asia has "Bangladesh, Bhutan, Cambodia ..."

       

      I need to combine these into a single file and re-sort the entire file alphabetically, but keeping the spreads together. Each page has just the country name as its title, which I can use with this script How to alphabetical order pages at indesign cs4? The problem is, it sorts the 2 pages inconsistently, so some right hand pages become left and some left become right. Is there a way to add this to the script?

        • 1. Re: Sort spreads alphabetically INDD CS6 or CC
          Jump_Over Level 5

          Hi,

           

          Assuming you are merging 2 files (single spread):

          - Angola, Benin

          - Argentina, Barbados

           

          What is your final goal? How your merged file should be sorted?

           

          Jarek

          • 2. Re: Sort spreads alphabetically INDD CS6 or CC
            sw52308871 Level 1

            Hi Jarek,

             

            The 4 files are all 2-page spreads. Each country has 2 pages of data, with a left and a right page (the data includes tables and charts). My final goal is to have all of the spreads in alphabetic order - so it would be "Angola, Argentina, Bangladesh, Barbados, Belize, Benin, Bhutan, Botswana, Cambodia etc." - with 2 pages for each.

             

            The problem is the 2-page spreads. Each file has a 2-page master, with mirrored margins and wider inside margins than outside. Teh left-hand pages need to stay on the left, followed by the right-hand pages. Each page has the country name at the top, in a paragraph style (so Angola has "Angola" at the top of both of the 2 pages).

             

            Sam

            • 3. Re: Sort spreads alphabetically INDD CS6 or CC
              Jump_Over Level 5

              Hi,

               

              OK. I missed you wrote '2-page spreads with per-country', sorry.

              Have you made any try with a code? How your files become merged?

              Is the only way to find 'country' as a top page textFrame content?

              What about page names? What about paragraphStyle name? Do your 'country' (and ONLY your 'country') use a specific style?

               

              Jarek

              • 4. Re: Sort spreads alphabetically INDD CS6 or CC
                sw52308871 Level 1

                Thanks Jarek,

                 

                I've merged the files manually - there are only 4.

                 

                I've tried with this script, which works, but the spreads are often swapped over, with right-hand pages moved to the left: "Script to sort Pages according to alphabetical order of their top TextFrames contents" by Trevor at https://forums.adobe.com/message/6115260#6115260

                 

                Yes, the "country" title has its own, unique paragraph style, so I'll try paragraphStyle instead of top page textFrame. Thanks for the suggestion (I'm not at the right Mac here, but will let you know when I've tried)

                 

                Sam

                • 5. Re: Sort spreads alphabetically INDD CS6 or CC
                  Trevorׅ Adobe Community Professional

                  Well as the world authority on that script does this one do what you want?

                  // Script to sort spreads according to alphabetical order of their top TextFrames contents  
                  // by Trevor (www.creative-scripts.com) custom and readymade scripts for InDesign  
                  // https://forums.adobe.com/thread/2354151
                  // http://forums.adobe.com/thread/1405293?tstart=0  
                  // see also http://forums.adobe.com/thread/1082984?tstart=0 for a better workflow  
                  
                  app.doScript("aToZPageSort()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Sort Pages By top text contents");
                  
                  function aToZPageSort() {
                      var doc, myPageIndex, l, topFrameIndex, z, pageFramesTops, i, topFrameHeight, getSpread;
                      doc = app.documents[0];
                      myPageIndex = [];
                      l = doc.spreads.length;
                      topFrameIndex = z = 0;
                      getSpread = function(f) {
                          var saftey = 100;
                          while ((f.constructor !== Spread) && saftey--) {
                              f = f.parent;
                          }
                          return f;
                      };
                      while (l--) {
                          pageFramesTops = doc.spreads[l].textFrames.everyItem().getElements().slice(0);
                          n = i = pageFramesTops && pageFramesTops.length;
                          if (!i) continue;
                          while (n--) pageFramesTops[n] = pageFramesTops[n].geometricBounds[0]; // for some reason when one trys to get the geometricBounds[0] from teh everyItems without the elements it return y1, x1 and not just y1 ?-)  
                          topFrameHeight = pageFramesTops[0];
                          topFrame = doc.spreads[l].textFrames[0];
                          for (var n = 0; n < i; n++) {
                              if (pageFramesTops[n] < topFrameHeight) {
                                  topFrameHeight = pageFramesTops[n];
                                  topFrame = doc.spreads[l].textFrames[n];
                              }
                          }
                          myPageIndex[z++] = {
                              spread: getSpread(topFrame),
                              contents: topFrame.contents.toLowerCase()
                          };
                      }
                      myPageIndex.sort(function(a, b) {
                          return (a.contents.toLowerCase() > b.contents.toLowerCase());
                      });
                  
                      l = myPageIndex.length;
                      while (l--) myPageIndex[l].spread.move(LocationOptions.AT_BEGINNING);
                  }
                  
                  

                   

                   

                  Trevor

                  1 person found this helpful
                  • 6. Re: Sort spreads alphabetically INDD CS6 or CC
                    Trevorׅ Adobe Community Professional

                    I do prefer this workflow

                     

                    /

                    /////////////////////////////////////////////////////////////////////////////////////
                    // Script by Trevor to sort Spreads according to alphabetical order of Page Headers //
                    // http://creative-scripts.com                                                      //
                    // https://forums.adobe.com/message/9686292#9686292                                 //
                    // adapted from http://forums.adobe.com/thread/1082984?tstart=0                     //
                    // make sure to change to the write paragraphs style                                //
                    // 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       //
                    //////////////////////////////////////////////////////////////////////////////////////
                    
                    app.doScript("aToZSpreadSort()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Sort Spreads text style contents");
                    
                    function aToZSpreadSort() {
                        var doc = app.documents[0],
                            myHeaders, l, getSpread;
                        getSpread = function(f) {
                            var saftey = 100;
                            while ((f.constructor !== Spread) && saftey--) {
                                f = f.parent;
                            }
                            return f;
                        };
                        app.changeGrepPreferences = app.findGrepPreferences = null;
                        app.findGrepPreferences.appliedParagraphStyle = "My Header";
                        // Change above to the name of header style  
                        myHeaders = doc.findGrep();
                        myHeaders.sort(function(a, b) {
                            if (a.contents.toLowerCase() > b.contents.toLowerCase()) return 1;
                            else if (a.contents.toLowerCase() < b.contents.toLowerCase()) return -1;
                            return 0;
                        });
                        l = myHeaders.length;
                        while (l--) getSpread(myHeaders[l].parentTextFrames[0]).move(LocationOptions.AT_BEGINNING);
                    }
                    
                    1 person found this helpful
                    • 7. Re: Sort spreads alphabetically INDD CS6 or CC
                      Jump_Over Level 5

                      Hi,

                       

                      I suggest something similar but different findText() target -- just to decrease count of moved Spreads:

                       

                      var
                           mDoc = app.activeDocument,
                           mSpreads = mDoc.spreads.everyItem().getElements(),
                           mSpreadsArray = [],
                           mParaStyle = mDoc.paragraphStyles.item("Title"),
                           cFind, cCountry, cSpread, step;
                      // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
                      app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "sortSpreads" );
                      function main () {
                          app.findTextPreferences = null;
                          app.findTextPreferences.appliedParagraphStyle = mParaStyle;
                          while (cSpread = mSpreads.pop()) {
                              cFind = cSpread.textFrames.everyItem().findText();
                              step = cFind.length;
                              cFind.sort();
                              while (step--) {
                                  if (!cFind[step].length) continue;
                                  cCountry = cFind[step][0].contents;
                                  mSpreadsArray.push([cCountry, cSpread]);
                                  break;
                                  }
                              }
                          mSpreadSort(mSpreadsArray);
                          app.findTextPreferences = null;
                          // |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
                          }
                      function mSpreadSort (cSpreadsArray) {
                           cSpreadsArray.sort(sortDoubleArray);
                           var cSpread;
                           while (cSpread = cSpreadsArray.pop()) {
                                cSpread = cSpread[1];
                                cSpread.move(LocationOptions.AT_BEGINNING);
                                }
                           }
                      function sortDoubleArray(a,b) {
                           return a[0] > b[0];
                           }
                      

                       

                      Notice that var mParaStyle need to update a proper style name.

                       

                      Jarek

                      1 person found this helpful
                      • 8. Re: Sort spreads alphabetically INDD CS6 or CC
                        sw52308871 Level 1

                        Thanks so much Trevor!  That worked perfectly!!

                         

                        Now I'm going to have to parse it carefully so that I can learn something here.

                         

                        Sam

                        • 9. Re: Sort spreads alphabetically INDD CS6 or CC
                          sw52308871 Level 1

                          For some reason I couldn't get this to work in INDD CS6 - but I'm going to try it on CC. My files didn't have headers but I can see this would be a great way around the problem if the spreads didn't all have the key word in the top frame.

                           

                          Thanks Trevor.

                          • 10. Re: Sort spreads alphabetically INDD CS6 or CC
                            sw52308871 Level 1

                            Thanks for this Jarek!  Trevor's first reply solved my current issue, but I like the idea of reducing the number of moved spreads. I tried this on CS6, and will try it on CC too - I think I may have just not let it run long enough! I'll let you know how I go.