9 Replies Latest reply on Sep 5, 2012 7:33 AM by Aart_G

    Page numbering with words

    gerhardlanz

      Is it possible set up automated page numbering where the numbers are actually formatted as words instead of numbers?

      Eg. 'Four' instead of '4'

        • 1. Re: Page numbering with words
          Michael Gianino Level 4

          Maybe if you ran a script  that would read the number and convert it to words, but that's just a guess. The easiest thing to do would be to thread  the text frames that hold your page number together on the master page, but don't put the page number place holder in it. You would then make a text file that contains a sequence of numbers written out as words, and place that into the text frame on the first document page while holding the shift key. That will override the master-page text frames and place one number/word/words in each.

           

          If typing out all the page numbers seems like a lot of work (especially if it's a really long sequence) you could probably use Excel to help you out. If you want to go that way, post back and I'll give you some tips.

          • 3. Re: Page numbering with words
            gerhardlanz Level 1

            Definitely sounds like what I want do. I welcome any assistance you can

            provide.

            • 4. Re: Page numbering with words
              Peter Spier Most Valuable Participant (Moderator)

              Please trun off the auto-quote when replying by email. All responses are posted to the web.

               

              You should start by reading the link that Jongware posted above.

              • 5. Re: Page numbering with words
                [Jongware] Most Valuable Participant

                Down here is a complete Javascript, tested with CS4 but it ought to work with later versions as well. It works like this:

                 

                1. Use the placeholder code '#' in the your Master Page(s) to indicate where the page number -- as words -- should appear. Extra text around it and formatting etc. should work just fine; you can even use something like

                 

                This is page # of my very first book

                 

                and the script will change the '#' to the full wording, maintaining any formatting you applied to it (in this case, bold and underlined) for just the page number text.

                A master pages based on yet another master page should work (and in fact I so hope you happen to have used that because I took care of that special case as well!).

                 

                2. Make sure this master page is applied to the pages in your document where you want the page number written out.

                3. The script works with both single-sided and double-sided master pages; it will not work with more than two pages per spread! (But that's a pretty unusual scenario.)

                4. Make sure the page numbering for these pages is in Roman numerals (1, 2, 3), not something like "a", "b", "c" -- the script needs the number to work.

                5. Run The Script. It will override the placeholder frame on each page and replace the placeholder text with English.

                6. If you moved pages around or changed the formatting on the master page, just run the script again and it will update everything. It will only update pages with this particular master page; if you applied another master page that doesn't contain the page number placeholder it will not remove the overridden frame from those pages!

                7. I wrote the function to convert number-to-text from scratch. Theoretically, it should be fine; then again, in practice the theory might not work. If you would like some other wording ("three-score and five", perhaps) just let me know & I'll see what I can do.

                8. The number-as-text is returned in all lowercase; per your example I change the first word to uppercase afterwards. However, Any Other Variant Is Possible, Of Course.

                 

                Enjoy!

                 

                (Copy, paste into a plain text editor such as the ESTK that comes with InDesign. Save into your User Scripts folder as "pageNumberWords.jsx" and it should appear in the Scripts panel. Double-click to run, then check if it worked as you hoped.)

                 

                //DESCRIPTION:Convert Page Number Placeholder '#' into Text
                // A Jongware Script 11-Dec-2011
                
                var allPages = app.activeDocument.pages;
                for (iterate=0; iterate<allPages.length; iterate++)
                {
                          customPageNumber (allPages[iterate]);
                }
                
                
                function customPageNumber (aPage)
                {
                          if (aPage.appliedMaster == null)
                                    return;
                
                
                          pageSide = (aPage.side == PageSideOptions.RIGHT_HAND) ? 1 : 0;
                          masterFrame = findFrameContaining (aPage.appliedMaster, pageSide, '#');
                          if (masterFrame != null)
                          {
                                    frame = findByLabel (aPage.pageItems, "page number");
                                    if (frame != null)
                                    {
                                              frame.removeOverride();
                                    }
                                    frame = masterFrame.override (aPage);
                                    frame.label = "page number";
                
                                    placeholder = frame.contents.indexOf ('#');
                                    if (placeholder != -1)
                                    {
                                              pageString = numberToText(Number(aPage.name));
                                              pageString = pageString.substr(0,1).toUpperCase()+pageString.substr(1);
                                              frame.characters[placeholder].contents = pageString;
                                    }
                          }
                }
                
                
                function findFrameContaining (master, side, text)
                {
                          var masterPage;
                          var i;
                
                          masterPage = master.pages[side];
                          for (i=0; i<masterPage.textFrames.length; i++)
                          {
                                    if (masterPage.textFrames[i].contents.indexOf(text) > -1)
                                              return masterPage.textFrames[i];
                          }
                          // Not found? Perhaps on this Master's Master?
                          if (master.appliedMaster != null)
                                    return findFrameContaining (master.appliedMaster, side, text);
                          return null;
                }
                
                
                // Needed because the very useful label lookup was
                // -- totally unnecessarily! -- removed in CS5+ ...
                function findByLabel (items, label)
                {
                          var i;
                          for (i=0; i<items.length; i++)
                                    if (items[i].label == label)
                                              return items[i];
                          return null;
                }
                
                
                function numberToText (number)
                {
                          var ones = [ "zero", "one", "two", "three", "four", "five",
                                    "six", "seven", "eight", "nine", "ten",
                                    "eleven", "twelve", "thirteen", "fourteen", "fifteen",
                                    "sixteen", "seventeen", "eighteen", "nineteen" ];
                          var tens = [ "zero", "ten", "twenty", "thirty", "fourty", "fifty",
                                    "sixty", "seventy", "eighty", "ninety" ];
                          var result = '';
                
                
                          if (number < 0)
                                    return "ha ha";
                          if (number == 0)
                                    return ones[0];
                
                
                          if (number >= 1000)
                          {
                                    thousand = Math.floor(number/1000);
                                    result = numberToText (thousand) + " thousand";
                                    number = number - 1000*thousand;
                                    if (number > 0)
                                              result = result + " ";
                          }
                
                
                          if (number >= 100)
                          {
                                    hundred = Math.floor(number/100);
                                    result = result + ones[hundred]+" hundred";
                                    number = number - 100*hundred;
                                    if (number != 0)
                                              result = result + " and ";
                          }
                          if (number >= 20)
                          {
                                    ten = Math.floor(number/10);
                                    result = result + tens[ten];
                                    number = number - 10*ten;
                                    if (number != 0)
                                              result = result + "-";
                          }
                          if (number != 0)
                                    result = result + ones[number];
                          return result;
                }
                
                • 6. Re: Page numbering with words
                  Peter Spier Most Valuable Participant (Moderator)

                  Very nice. I hope you don't mind if I suggest changing the spelling of "fourty" to "forty" though.

                  1 person found this helpful
                  • 7. Re: Page numbering with words
                    gerhardlanz Level 1

                    There appears to be an error on line 46:

                     

                    for (i=0; i<masterPage.textFrames.length; i++)

                     

                    ExtendScript is telling me there's an invalid object?

                    • 8. Re: Page numbering with words
                      [Jongware] Most Valuable Participant

                      Good thing I revisited this script just right now for someone else

                      I tested it on one of my own stack of documents and got the same error. No wonder I missed this particular error: it occurs when you have your document designed as spreads, but one of the master pages only has 1 page.

                       

                      Replace the function findFrameContaining with this one and try again.

                       

                      function findFrameContaining (master, side, text)
                      {
                                var masterPage;
                                var i;
                      
                                if (master.pages.length > 1)
                                          masterPage = master.pages[side];
                                else
                                          masterPage = master.pages[0];
                                for (i=0; i<masterPage.textFrames.length; i++)
                                {
                                          if (masterPage.textFrames[i].contents.indexOf(text) > -1)
                                                    return masterPage.textFrames[i];
                                }
                                // Not found? Perhaps on this Master's Master?
                                if (master.appliedMaster != null)
                                          return findFrameContaining (master.appliedMaster, side, text);
                                return null;
                      }
                      
                      1 person found this helpful
                      • 9. Re: Page numbering with words
                        Aart_G

                        Great Hip Hip to You

                        The script runs like Usain Bolt.

                        Thanx for the quick solution.

                        Aart