5 Replies Latest reply: May 21, 2012 11:43 AM by Dirk Becker RSS

    Get page of current XML element?

    islander1970 Community Member

      Hello all,

       

      I am using CS5.5 and have a script (I'm using Javascript) that is placing XML elements into a text frame that is linked and spans several pages. What I'm trying to do is find out what the current page is where the current element is being inserted. Say I start on page, 10. Once the content gets to page 11, I'm want to know that I'm on page 11 with those elements as certain ones are being placed into a frame (not inline) and I want that frame to be placed on the proper page.

       

      I've tried a lot of different things, and seem very close, but can't quite get it. Is there a way to tell what page an XML element is being inserted on?

       

      Any advice/help is greatly appreciated.

       

      Thanks.

        • 1. Re: Get page of current XML element?
          Dirk Becker  Community Member

          XML Elements are not associated with a particular page, e.g. there can be elements without layout, or the root element cover all 3000 pages. For XML Elements holding text you can ask any insertion point, such as the first and the last for their parent text frames. The parent text frames property of an insertion point usually has only one entry, only the insertion points at the crossing of chained frames live in two frames.

           

          Dirk

          • 2. Re: Get page of current XML element?
            islander1970 Community Member

            I can't seem to get the insertion point of the parent text frame. I've added alerts to pause the process and everything is being written on the page as expected, I just can't retrieve the current page being written to.

             

            What I'm trying to get at might look something like this:

             

            var pageIndex = myElement.parent.<Current Insertion Point>;
            var currentPage = myDocument.pages.item(pageIndex);

            • 3. Re: Get page of current XML element?
              Dirk Becker  Community Member

              You have to get the insertion points from the XMLElement. The insertion point leads to the frame and that to the page.

               

              ( function () {

                  var rootXE = app.activeDocument.xmlElements.item(0);

                  var articleXE = rootXE.xmlElements.item(0);

                  var storyXE = articleXE.xmlElements.item(0);

                  var yourXE = storyXE.xmlElements.item(0);

                  var startIP = yourXE.insertionPoints.firstItem();

                  var endIP = yourXE.insertionPoints.lastItem();

                  var endTF = endIP.parentTextFrames[0];

                  var endPage = endTF.parentPage;

                  $.writeln("yourXE ends on page "+endTF.parentPage.name);

              } ) ();

              • 4. Re: Get page of current XML element?
                islander1970 Community Member

                Thank you for your help. That is very similar to where I have been in trying to get this to work. Using your snippet to try to echo the page, I'm running into a problem though. The insertion points do return insertion points, but the next call to parentTextFrames doesn't return a value. Here is a subsection of what I have while trying to get this to work:

                 

                function ProcessDisplayAds(){

                  

                    this.name = "ProcessDisplayAds";

                    this.xpath = "//mvtClassDisplayAd";

                    this.apply = function(myElement, myRuleProcessor){

                       

                        with(myElement){

                 

                            try {

                                // Commented because we already have myElement from the rule processing

                                /*var rootXE = app.activeDocument.xmlElements.item(0);

                                var articleXE = rootXE.xmlElements.item(0);

                                var storyXE = articleXE.xmlElements.item(0);

                                var yourXE = storyXE.xmlElements.item(0);*/

                 

                                var startIP = myElement.insertionPoints.firstItem();

                                var endIP = myElement.insertionPoints.lastItem();

                                // The above DO return insertion points

                                var endTF = endIP.parentTextFrames[0];

                                alert(endTF);

                                // Doesn't return anything

                               

                                /*    var endPage = endTF.parentPage;

                                alert("myElement ends on page "+endTF.parentPage.name);

                               */

                            } catch(e){};

                        }   

                        return true;

                    }

                }

                 

                The XML structure is simply:

                <Root>

                     <ClassifiedAds>

                          <mvtClassDisplayAd></mvtClassDisplayAd>

                     </ClassifiedAds>

                </Root>

                 

                Where ClassifiedAds is tagged to the main text frame, so there is a parent text frame, or should be any way.

                 

                Any ideas why I'm not getting the reference to it?

                 

                Thank you again for your help.

                • 5. Re: Get page of current XML element?
                  Dirk Becker Community Member

                  If any text (such as your insertion point) is in overset rather than in a text container (such as a text frame) then you don't have a parent text frame. Instead of testing for that condition (parentTextFrames array has length of 0), you may also check the parent story, e.g. yourIP.parentStory.overflows.

                   

                  Dependent from your template / document InDesign also has a feature that may automatically add pages. If I remember that correctly, it is only triggered during composition. You would then force it with yourIP.parentStory.recompose().

                   

                  Dirk