9 Replies Latest reply on Jun 25, 2012 5:12 AM by Cenchen

    How do I get a page number for a story?

    TRuschak1

      If I am looping through the stories in a document, and I want to know what page the story is on, how would I find that out?  This seems as if it would be the simplest thing in the world, but I have been up and down the object model for hours, and I am having no luck.

       

      I must be missing something.  Can some kind soul here help me?

       

      Thanks,

      Thomas Ruschak

        • 1. Re: How do I get a page number for a story?
          [Jongware] Most Valuable Participant

          What version of InDesign? For any modern version, this ought to be enough:

           

          alert (app.stories[0].textContainers[0].parentPage.name);

           

          -- note that it will only report the first page of a story.

           

          TRuschak1 wrote:

           

          ... what page the story is on ...

           

          .. because a single story can run over many pages.

          • 2. Re: How do I get a page number for a story?
            TRuschak1 Level 1

            Sorry, my bad, I am on CS6.  I tried your suggestion in a js script alone.  When I ran it, I got this:

             

            Snap1.jpg

             

            I usually do things in VBScript, just a bit more familiar with it.  However, if javascript is necessary, I am happy to use that.

             

            Again, all I want is to be able to say what page a given story is on.  I'm happy to get the first page if it is on more than one.  I have spent the night being amazed at how hard that simple request is in InDesign.

             

            Anyway, if you can, have pity on me and help some more

             

            Tom

            • 3. Re: How do I get a page number for a story?
              [Jongware] Most Valuable Participant

              My bad, 'stories' are not part of 'app'. Try

               

              alert (app.activeDocument.stories[0].textContainers[0].parentPage.name);

              • 4. Re: How do I get a page number for a story?
                TRuschak1 Level 1

                Hmm.. That didn't work either.  This time it gave me the following:

                 

                Snap2.jpg

                 

                Here's a piece of my (as yet incomplete) script.  I am assembling a TOC, but in a very particular way.  There are text boxes with part numbers which are immediately followed by a description.  The part numbers are marked with the paragraph style "Kit Number" and the descriptions are marked with "Kit Desc".  As it is, the regular InDesing TOC will assemble the Part number + the desc and put them both on the same line with the page number, and that's well and good, but the clients also want it sorted by the description.  So I go through the doc looking for those two paragraph style and adding to arrays when I find them:

                 

                I have a loop through the documents and document stories above... nothing to see.   Then, I go on:

                 

                            Set myStory = myDoc.Stories(j)
                            k = 1
                            DoWhile k <= myStory.Paragraphs.Count
                               If myStory.Paragraphs(k).appliedParagraphStyle = "Kit Number"Then
                                  s1 = myStory.Paragraphs(k).contents
                                  s1 = Left(s1,Len(s1)-1)
                                  Start = 1
                               EndIf
                               If myStory.Paragraphs(k).appliedParagraphStyle = "Kit Desc"and Start = 1Then
                                  s2 = myStory.Paragraphs(k).contents
                                  ReDimPreserve aTocNumber(UBound(aTocNumber) + 1)
                                  ReDimPreserve aTocDesc(UBound(aTocDesc) + 1)
                                  ReDimPreserve aTocPage(UBound(aTocPage) + 1)
                                  aTocNumber(UBound(aTocNumber)) = s1
                                  aTocDesc(UBound(aTocDesc)) = s2
                ==>               q = myStory.textContainers(0).parentPage.name
                                  aTocPage(UBound(aTocPage)) = q
                                  Start = 0
                               EndIf

                               k = k + 1
                            Loop

                I am guaranteed by the person I am doing it for (who happens to be my lovely wife, so I have to believe her) that there will always be a Kit Dec following each Kit Number.  The code above works correctly, so if I could get the line marked with a ==> to work, I would be ok.  I know that myStory is ok, since I can get the paragraph & contents from it, so as far as I can see, the problem with this current one is that textcontainers(0) is null.

                 

                Can you help?

                 

                Tom

                • 5. Re: How do I get a page number for a story?
                  TRuschak1 Level 1

                  Sorry, I forgot to mention that I checked myStory.textContainers.count at the time of the ==> marked line, and it is 1, so textContainers(0) should work in VBScript, right?  When I run my script the line it complains about is the one I based on your format, and it gives a different error:

                   

                  Snap3.jpg

                  • 6. Re: How do I get a page number for a story?
                    [Jongware] Most Valuable Participant

                    Maybe you have to start counting at 1 for VBS?

                     

                    I know, it's weird. Most computer languages start counting at 0, the proper way ;)

                    • 7. Re: How do I get a page number for a story?
                      TRuschak1 Level 1

                      Unfortunately no (I'd be happy if it was that simple).  VBS arrays always start at 0, the natural way :-)

                       

                      Is there a debugger for these InDesign scripts, so I can examine variables?  If so, how would I start it, and where is it?

                       

                      Also, the other thing is that I tried what you gave me in a javascript, and it didn't work either:

                      (alert (app.activeDocument.stories[0].textContainers[0].parentPage.name);

                       

                      (see previous post for the error it made)

                       

                      I guess I am just stunned by how difficult this is.  Am I really the first person to need this?

                       

                      Thanks!

                      Tom

                      • 8. Re: How do I get a page number for a story?
                        [Jongware] Most Valuable Participant

                        TRuschak1 wrote:

                         

                        Is there a debugger for these InDesign scripts, so I can examine variables?  If so, how would I start it, and where is it?

                         

                        This is called the ESTK -- ExtendScript Toolkit -- and you get installed it for free with your InDesign. It's probably lurking somewhere in your local Programs or Applications folder.

                         

                        I hate it, though, and use my wits to debug. Plus lots of intermediate alerts. </personal_view>

                         

                        I'm not that used to CS5 and its new 'parentPage' function (I can make do without ) so I just fired it up to test with. I was assuming perhaps your first story could be on a Master Page, rather than a regular page, and so 'parentPage' might be choking on that. It didn't -- apparently, the 'name' of a Master Page is its prefix -- so I had to gues a bit further.

                         

                        Normally, I would take a composite command that fails apart to its constituent elements, in the general vein of

                         

                        a = app.activeDocument; alert (a);

                        b = a.stories; alert (b);

                         

                        ... and so on, so you would get an error message for exactly the command that was failing, but in this case lateral thinking seemed to be enough:

                         

                        In this scriptlet fragment, 'parentPage' is null if the first story in your file starts on the pasteboard. Attempting to access the name causes that exact error you are seeing.

                         

                        TRuschak1 wrote:

                        I guess I am just stunned by how difficult this is.  Am I really the first person to need this?

                         

                        Probably not But you might be the first to have tried it with a story on the pasteboard.

                         

                        Javascript's object model mimicks the actual internal document model very close, better than AppleScript or VBS. But "the internal document model" is very, very large, extensive, and with hidden depths. It can take some time to eat your way through the outer layers of this smelly onion, drilling down to what you actually want to know.

                        • 9. Re: How do I get a page number for a story?
                          Cenchen Level 2

                          Hi,    

                                    Getting stories page number,

                           

                                        

                          var doc = app.activeDocument
                          var story = app.activeDocument.stories;
                          var st = [];
                          var s = "";
                          for(var i=0;i<story.length;i++)
                          {
                            
                              if((s.match( story[i].textContainers[0].parentPage.name + "(,|$)")) == null)
                              {
                                  st.push(story[i].textContainers[0].parentPage.name);
                                
                                  s = s + story[i].textContainers[0].parentPage.name + ",";
                              }

                          }
                          if(st.length > 0)
                          alert(st,"Stories found in below pages")
                          else
                          alert("No Story found")