13 Replies Latest reply on Feb 14, 2008 11:50 AM by (Avi)

    Problem in fetching last page number.

      Hi all my name is Avi. I am new to this forum. I have some problem regarding javascript. How to fetch last page number.

      I created a Text field using javascript which is as follows

      var myTextFrame4 = myDocument.pages.item(0).textFrames.add();
      myTextFrame4.geometricBounds = ["38p6", "21p0", "39p3", "30p0"];
      myTextFrame4.contents = "5Page ";
      myTextFrame4.parentStory.insertionPoints.item(-1).contents = SpecialCharacters.autoPageNumber;
      myTextFrame4.parentStory.insertionPoints.item(-1).contents = " of ??6";

      In above code i am using autopagenumbering. My problem is i want to fetch last page number and change ?? to that page number.
      For Example i am inserting text in document and 5 page will be generated by using autoPageNumber the text field will chage to page 1 of ?? .....
      and at last page 5 of ??. What i want to change the ?? to 5(which is last page when you insert te text). I want replace the ?? to last page number.

      There is one page properties which is length which gives you how many number of pages in the collection but i am not able to use it properly to get the expected output.

      What kind of change i need to make in my script to get the expected output.
      I knew this will be difficult to fetch last page number but i hope i will get my answer for this forum.

      Thank you,

      --Avi
        • 1. Re: Problem in fetching last page number.
          Level 1
          I'm not JS man but I'll try to help you

          change line:
          myTextFrame4.parentStory.insertionPoints.item(-1).contents = SpecialCharacters.autoPageNumber;

          to

          myTextFrame4.parentStory.insertionPoints.item(-1).contents = myTextFrame4.parent.name;

          you need to check ".parent.name" - JS is CaseSensitive


          and description from VB ObjectBrowser:

          Property Parent As Variant
          read-only
          Member of InDesign.TextFrame
          The TextFrame's parent as Spread, MasterSpread, PageItem, Oval, Rectangle, Polygon, GraphicLine, Group, State, Document, Layer, Page, Button, TextFrame, Story, Text, Character, Word, Line, TextColumn, Paragraph, TextStyleRange, Cell, Table, InsertionPoint or Footnote

          you should find same info in ESTK ...

          robin

          --
          www.adobescripts.com
          • 2. Re: Problem in fetching last page number.
            Level 1
            How can this help me to change ?? to page number.
            • 3. Re: Problem in fetching last page number.
              Martin Fischer Level 2
              Avi,

              in CS3 you may make a new text variable with type of last page number with document scope:

              <pre>
              d = app.activeDocument;
              v = d.textVariables.add( {name:'Last Page', variableType:VariableTypes.LAST_PAGE_NUMBER_TYPE} );
              v.variableOptions.scope = VariableScopes.DOCUMENT_SCOPE;
              </pre>

              Now you will have to insert the text variable at your insertion point.

              But how to insert a text variable?

              "myInsertionPoint.contents = v;" will not work.

              Martin Fischer
              • 4. Re: Problem in fetching last page number.
                Martin Fischer Level 2
                Avi,

                you can add a text variable to an insertion point like this (given myInsertionPoint and myTextVariable):

                > myInsertionPoint.textVariableInstances.add( {associatedTextVariable : myTextVariable} );

                Martin Fischer
                • 5. Re: Problem in fetching last page number.
                  Level 1
                  oh sorry,

                  change line:
                  myTextFrame4.parentStory.insertionPoints.item(-1).contents = " of ??6";

                  to:

                  myTextFrame4.parentStory.insertionPoints.item(-1).contents = " of ";
                  myTextFrame4.parentStory.insertionPoints.item(-1).contents = app.activeDocument.pages.item(-1).name;

                  ignore my earlier post

                  robin

                  --
                  www.adobescripts.com
                  • 6. Re: Problem in fetching last page number.
                    Level 1
                    Hello Robin

                    Thank you for your post but the problem is when i am trying to entered the text then it will create 11 page .But still it showing text field page 1 of 1 and for the next page page 2 of 1 ...... at last page 11 of 1. But this is not the output i want.

                    I want page 1 of 11 and for next page 2 of 11.......at last page 11 of 11.

                    Thank you.
                    • 7. Re: Problem in fetching last page number.
                      Level 1
                      can you place again this part of your script ??

                      robin

                      --
                      www.adobescripts.com
                      • 8. Re: Problem in fetching last page number.
                        Peter Kahrel Adobe Community Professional & MVP
                        Avi,

                        >There is one page properties which is length which gives you how many number of pages in the collection but i am not able to use it properly to get the expected output.

                        myDocument.pages.length

                        should give you the number of pages in the document. Why does that not work for you?

                        Alternatively, if you're using CS3 you could use MArtin's suggestion.

                        Peter
                        • 9. Re: Problem in fetching last page number.
                          Level 1
                          var myTextFrame4 = myDocument.pages.item(0).textFrames.add();
                          myTextFrame4.geometricBounds = ["38p6", "21p0", "39p3", "30p0"];
                          myTextFrame4.contents = "5Page ";
                          myTextFrame4.parentStory.insertionPoints.item(-1).contents = SpecialCharacters.autoPageNumber;
                          //myTextFrame4.parentStory.insertionPoints.item(-1).contents = " of ??6";
                          myTextFrame4.parentStory.insertionPoints.item(-1).contents = " of ";
                          myTextFrame4.parentStory.insertionPoints.item(-1).contents = app.activeDocument.pages.item(-1).name;
                          myTextFrame4.parentStory.insertionPoints.item(-1).contents = " 6";

                          This the script which i am using but still i am not getting correct output. What i want is to when i entred the text using shift + ctrl (MAC OS X) on that page it will generate suppose 10 page then i need to change ?? to 10(Number of pages generated)

                          I tried all the suggestion given by this forum but not a single one is working fine for me.
                          • 10. Re: Problem in fetching last page number.
                            Level 1
                            remove last line and should be ok

                            robin

                            --
                            www.adobescripts.com
                            • 11. Re: Problem in fetching last page number.
                              Level 1
                              When you remove that line it sill showing a square box.
                              I want to change a the number of page when you close the program.
                              • 12. Re: Problem in fetching last page number.
                                Level 1
                                Avi:

                                I use this script every day for the task you are describing.
                                I set a keyboard shortcut to call the script so I can update it frequently as needed.

                                Follow these steps:

                                1. Type out the words "Page 1 of X"

                                2. Add a new character style to your document named "LastPageNumber" and apply the character style to just that character "X". NOTHING ELSE!

                                3. Run the script from the scripts panel or set a keyboard shortcut to run the script.

                                The last page number will update after you run the script.

                                Copy and paste this code into a jsx file and save it in your scripts folder.

                                var myDoc = app.activeDocument;
                                var myPageCount = myDoc.pages.length + "";

                                var myStyle = myDoc.characterStyles.item("LastPageNumber");
                                app.findPreferences = app.changePreferences = null;

                                myDoc.search("",false,false,myPageCount,{appliedCharacterStyle:myStyle});

                                app.findPreferences = app.changePreferences = null;

                                Good Luck!

                                TJV
                                • 13. Re: Problem in fetching last page number.
                                  Level 1
                                  Thank you.
                                  This is the perfect ans what i am looking for.
                                  Can you give me your email id. So, if i have any doubt i can contact directly.