11 Replies Latest reply on Nov 18, 2013 7:20 AM by VWu

    Creating Directory - Showing Listing Count on All Directory Pages

    VWu Level 1

      Hi to all InDesign Scripters

       

      I am currently creating a directory with InDesign CS6, which contains the name of the person, address, cities, etc.

      I have a question which is about showing the listing count per city,

      for example, there are 30 names under a city (across spreads and pages),

      how to show the number of listing count on InDesign automatically?

      And how to make InDesign pick up what numebrs to use on that page?

      (If I have two pages should show total listing count under the same city is 30)

       

      I found a script to add sequential # to the paragraph styles, however I was only able to pick up the last number on the page using text variable -Running Headers function in InDesign, not the last number under the same city.

       

      Any help would be appreciated!

       

       

      Best Regards

      V

        • 1. Re: Creating Directory - Showing Listing Count on All Directory Pages
          Trevorׅ Adobe Community Professional

          Sounds easy,

           

          Send a precise screenshot (you can change the persornal details :-) of how a page is set up including where you want the counting to be

          • 2. Re: Creating Directory - Showing Listing Count on All Directory Pages
            VWu Level 1

            Thank you for your response Trevo!!

             

            The directory looks like attached pages. The first city is Asteria and the second is Bell Gardens

            How to write a script to get the total number of names/listings under a city?

            and tell page two to show the exact same total listing # (when the pages are still under the same city)

            template.pngtemplate2.png

            • 3. Re: Creating Directory - Showing Listing Count on All Directory Pages
              Trevorׅ Adobe Community Professional

              Hi VWu

               

              Presuming that you only have at most one city per page and you use a unique paragraph style for you city titles and presuming that you use a paragraph style for you name entries then the following should do it.

               

              Trevor

               

               

              // City Listings Count Script by Trevor www.creative-scripts.com (Coming soonish)
              // Trevor {at} creative-scripts {dot} com
              // http://forums.adobe.com/message/5837823#5837823
              
              app.doScript("addListingsCount()", ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Add Listings Count");
              
              function addListingsCount () {
                  var  doc = app.activeDocument,
                          cityParagraphStyle = doc.paragraphStyles.itemByName ("City Title"), // Change to correct paragraphStyles name
                          nameParagraphStyle = doc.paragraphStyles.itemByName ("Entry Name"), // Change to correct paragraphStyles name
                          countParagraphStyle = doc.paragraphStyles.itemByName ("Entries Count"), // Change to correct paragraphStyles name
                          cityFinds = [],
                          entryFinds = {};
              
              
                  // Get GeometricBound for Title Text Frames
                  var   pageMargins = [], pageGeos = [], textFrameGeos = [],
                           vp = doc.viewPreferences.verticalMeasurementUnits,
                           distanceFromTopMarginToTheTopOfTheEntriesCountTextFrame = UnitValue (".8cm").as(vp);  // Change distance as needed 
                           heigtOfTheEntriesCountTextFrame = UnitValue (".5cm").as(vp);  // Change height as needed 
                  pageMargins[0] = doc.pages[0].marginPreferences;
                  pageGeos[0] = doc.pages[0].bounds;
                  textFrameGeos[0] = pageGeos[0];
                  textFrameGeos[0][0] += pageMargins[0].top - distanceFromTopMarginToTheTopOfTheEntriesCountTextFrame;
                  textFrameGeos[0][1] += pageMargins[0].left;
                  textFrameGeos[0][2] = textFrameGeos[0][0] + heigtOfTheEntriesCountTextFrame;
                  textFrameGeos[0][3] -= pageMargins[0].right;
              
                  pageMargins[1] = doc.pages[1].marginPreferences;
                  pageGeos[1] = doc.pages[1].bounds;
                  textFrameGeos[1] = pageGeos[1];
                  textFrameGeos[1][0] += pageMargins[1].top - distanceFromTopMarginToTheTopOfTheEntriesCountTextFrame;
                  textFrameGeos[1][1] += pageMargins[1].left;
                  textFrameGeos[1][2] = textFrameGeos[1][0] + heigtOfTheEntriesCountTextFrame;
                  textFrameGeos[1][3] -= pageMargins[1].right;
              
                  app.changeTextPreferences = app.findTextPreferences = null;
                  app.findTextPreferences.appliedParagraphStyle = cityParagraphStyle;
                  cityFinds = doc.findText();
                  app.findTextPreferences.appliedParagraphStyle = nameParagraphStyle;
              
                  var l = cityFinds.length,
                        c = 0, nameFinds, pageOffsets = [], pageOffset, fl, city;
              
                  while (c < l) {
                      pageOffset = cityFinds[c].parentTextFrames[0].parentPage.documentOffset;
                      nameFinds = ([]).concat.apply ([],doc.pages [pageOffset].textFrames.everyItem().findText ());
              
                      city = cityFinds[c].contents;
                      fl = nameFinds.length;
                      pageOffsets [c] = pageOffset;
                      entryFinds [city] = (entryFinds [city]) ? entryFinds [city] + fl : fl;
                      c++
                      }
              
                  while (l--) doc.pages[pageOffsets[l]]
                                        .textFrames.add ({
                                             geometricBounds: textFrameGeos [pageOffsets[l] % 2],
                                             contents: "Total Listings: " + entryFinds [cityFinds[l].contents],
                                             name: "Listings Count"
                                             });
                  doc.pages.everyItem().textFrames.itemByName ("Listings Count").texts[0].appliedParagraphStyle = countParagraphStyle;
              }
              
              // Note if you ever want to remove these text frame you can do
              // app.activeDocument.pages.everyItem().textFrames.itemByName ("Listings Count").remove()
              
              • 4. Re: Creating Directory - Showing Listing Count on All Directory Pages
                VWu Level 1

                Hi Trevo,

                 

                Amazing work! Really apprciate you for your help!

                Wish I can be as talented and skillful as you one day!

                I'm still doing some tests on the script and there are stilll some errors because of my document settings.

                I will try to dig it out by myself first

                • 5. Re: Creating Directory - Showing Listing Count on All Directory Pages
                  Trevorׅ Adobe Community Professional

                  Glad to have helped and welcome to the forum.

                  If you need more help the just leave a post.

                   

                  Can you please mark the answer as correct instead of helpful.

                   

                  Thanks

                  Trevor

                  • 7. Re: Creating Directory - Showing Listing Count on All Directory Pages
                    VWu Level 1

                    Hi Trevor,

                     

                    I've been trying to edit the script/document but somehow I am still getting error "invalid parameter" on .textFrames.add ({

                    I am making a guess it is because I put City Title on master pages and using text variable function to pick up the City Title from Address on the same page, however even if I type and paste the City Tltle directly on the pages, nothing changed.

                     

                    I am posting my document on

                    https://drive.google.com/file/d/0B1aF_BZrfvR1Rm1HcVpWZW8zMVE/edit?usp=sharing

                    Screen Shot 2013-11-15 at 12.01.30 AM.png

                     

                    Any help will be apprecited!

                    • 8. Re: Creating Directory - Showing Listing Count on All Directory Pages
                      Trevorׅ Adobe Community Professional

                      Hi V,

                       

                      I looked at the file you attached.

                      You first need to remove the text boxes that you put in which contain "Total Listings:" in them.

                      You can run this to do that

                       

                      var  doc = app.activeDocument,
                                  finds, l;
                      app.changeGrepPreferences = app.findGrepPreferences = null;
                      app.findGrepPreferences .findWhat = "\\ATotal Listings:\\s*\\Z";
                      app.doScript("addListingsCount()", ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Add Listings Count");
                      function addListingsCount () {
                          finds = doc.findGrep();
                          l = finds.length;
                          while (l--) finds[l].parentTextFrames[0].remove();
                          }
                      

                       

                      After you've removed those frames run the below script and it should work for your document.

                       

                      // City Listings Count Script by Trevor www.creative-scripts.com (Coming soonish)
                      // Trevor {at} creative-scripts {dot} com
                      // http://forums.adobe.com/message/5837823#5837823
                      
                      
                      
                          var  doc = app.activeDocument,
                                  cityCharacterStyle = doc.characterStyles.itemByName ("City"), // Change to correct paragraphStyles name
                                  nameParagraphStyle = doc.paragraphStyles.itemByName ("Entry Name"), // Change to correct paragraphStyles name
                                  countParagraphStyle = doc.paragraphStyles.itemByName ("Entries Count"), // Change to correct paragraphStyles name
                                  cityFinds = [],
                                  entryFinds = {};
                      
                      
                          // Get GeometricBound for Title Text Frames
                          var   pageMargins = [], pageGeos = [], textFrameGeos = [],
                                   vp = doc.viewPreferences.verticalMeasurementUnits,
                                   distanceFromTopMarginToTheTopOfTheEntriesCountTextFrame = UnitValue ("23.625pt").as(vp);  // Change distance as needed 
                                   heigtOfTheEntriesCountTextFrame = UnitValue (".5cm").as(vp);  // Change height as needed 
                                   widthOfTheEntriesCountTextFrame = UnitValue ("153.75pt").as(vp);  // Change width as needed 
                                   pageWidth = doc.pages[0].bounds[3];
                      
                          app.changeTextPreferences = app.findTextPreferences = null;
                          app.findTextPreferences.appliedCharacterStyle = cityCharacterStyle; // keep grep set out of fast entire undo mode for safety reasons
                          
                      app.doScript("addListingsCount()", ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Add Listings Count");
                          
                      function addListingsCount () { 
                          pageMargins[0] = doc.pages[0].marginPreferences;
                          textFrameGeos[0] = [];
                          textFrameGeos[0][0] = pageMargins[0].top - distanceFromTopMarginToTheTopOfTheEntriesCountTextFrame;
                          textFrameGeos[0][1] = pageWidth - pageMargins[0].right - widthOfTheEntriesCountTextFrame;
                          textFrameGeos[0][2] = textFrameGeos[0][0] + heigtOfTheEntriesCountTextFrame;
                          textFrameGeos[0][3] = pageWidth - pageMargins[0].right;
                      
                          pageMargins[1] = doc.pages[1].marginPreferences;
                          textFrameGeos[1] = [];
                          textFrameGeos[1][0] = pageMargins[1].top - distanceFromTopMarginToTheTopOfTheEntriesCountTextFrame;
                          textFrameGeos[1][1] = pageWidth*2 - pageMargins[1].right - widthOfTheEntriesCountTextFrame;
                          textFrameGeos[1][2] = textFrameGeos[1][0] + heigtOfTheEntriesCountTextFrame;
                          textFrameGeos[1][3] = pageWidth*2 - pageMargins[0].right;
                          cityFinds = doc.pages.everyItem().textFrames.everyItem().findText();
                      
                          var l = cityFinds.length,
                                c = 0, nameFinds, pageOffsets = [], pageOffset, fl, city, firstFind, n=0, theCities = [];
                      
                          while (l > c++) {
                              nameFinds = ([]).concat.apply ([],cityFinds[c]);
                              if (!(fl = nameFinds.length)) {continue;}
                              firstFind = nameFinds[0];
                              pageOffset = firstFind.parentTextFrames[0].parentPage.documentOffset;
                              theCities[n] = city = firstFind.contents;
                              pageOffsets [n++] = pageOffset;
                              entryFinds [city] = (entryFinds [city]) ? entryFinds [city] + fl : fl;
                              
                              }
                      
                          while (n--) {doc.pages[pageOffsets[n]]
                                                .textFrames.add ({
                                                     geometricBounds: textFrameGeos [((pageOffsets[n] !=0) + pageOffsets[n]) % 2],
                                                     contents: "Total Listings: " + entryFinds [theCities[n]],
                                                     name: "Listings Count"
                                                     });
                                             }
                      }
                          doc.textFrames.itemByName ("Listings Count").isValid && doc.pages.everyItem().textFrames.itemByName ("Listings Count").texts[0].appliedParagraphStyle = countParagraphStyle; // keep isValid && out of fast entire undo mode for safety reasons
                      
                      // Note if you ever want to remove these text frame you can do
                      // app.activeDocument.pages.everyItem().textFrames.itemByName ("Listings Count").remove()
                      
                      • 9. Re: Creating Directory - Showing Listing Count on All Directory Pages
                        VWu Level 1

                        Hi Trevor,

                         

                        Thanks for your time! You are awesome!

                        I removed the text boxes I created and ran the script, the total listings on page 2 shows 34 (which is not the number I need) and page 3 shows 8 (which is correct). The city Artesia should have total lisintgs = 66 listings instead of 34 (page 1=33 listings and page 2=33 listings, the total for Artesia=66 listings), and 66 should show on both page 1 and 2. I will try to edit your script to match my need and I will ask you again if I still need help

                        • 10. Re: Creating Directory - Showing Listing Count on All Directory Pages
                          Trevorׅ Adobe Community Professional

                          Hi V

                           

                          Sorry about that

                          Change the 1st while loop to

                             while (l--) {
                                  nameFinds = ([]).concat.apply ([],cityFinds[c]);
                                  if (!(fl = nameFinds.length)) {c++; continue;}
                                  firstFind = nameFinds[0];
                                  pageOffset = firstFind.parentTextFrames[0].parentPage.documentOffset;
                                  theCities[n] = city = firstFind.contents;
                                  pageOffsets [n++] = pageOffset;
                                  entryFinds [city] = (entryFinds [city]) ? entryFinds [city] + fl : fl;
                                  c++;
                                  }
                          

                           

                          should work

                          • 11. Re: Creating Directory - Showing Listing Count on All Directory Pages
                            VWu Level 1

                            Yes, it works like a charm now!

                            Thank you for your help again!