2 Replies Latest reply on Jun 24, 2015 1:49 AM by mmmathur

    How to get corresponding page object for slug data?

    mmmathur Level 1

      Hi There,

       

      Hoping that there would be any solution for it, I'm pushing my question to your way - I have a InDesign file with a spread that has two pages, each page has it's own slug with page related information now what I need to get page object pertaining to that slug.

       

      As we all know that parentPage property returns null incase of pageItem is outside the page trim area.

       

      I tried something like this but it does not work here.

       

      function getPageNumber(curPageItem)
      {
      var doc =app.activeDocument;
      for(pageIndex=0; pageIndex < doc.pages.length; pageIndex++)
      {
      var curDocPage =doc.pages[pageIndex],
      pageBounds = curDocPage.bounds,
      pageWidth = pageBounds[3] - pageBounds[1];
      if(curPageItem.parentPage == null)
                              {
                                  var pageItemBounds = curPageItem.geometricBounds,
                                  itemX0Position = pageItemBounds[1];
                                  
                                  if((curPageItem.extractLabel(scriptLabels.LSLUG) == scriptLabels.LSLUG && 
                                  itemX0Position < pageWidth) ||
                                  (curPageItem.extractLabel(scriptLabels.RSLUG) == scriptLabels.RSLUG && 
                                  itemX0Position > pageWidth))
                                  {
                                      return var pgNum = curPageItem.contents;
                                  }
                              }
      }
      }
      

       

      Thanks in advance

      Mac

        • 1. Re: How to get corresponding page object for slug data?
          Marc Autret Level 4

          Hi Mac,

           

          I don't know what is your extractLabel() stuff about, but you can for sure identify the nearest page of some given page item.

          Do not run over the entire document, just consider the parent spread of the object and loop over its specific pages.

          For each page, we compute center-to-center distance along the x-axis and we simply keep the best match—that is, the lowest value—as shown below:

           

          var parentSpread = function F(/*DOM*/o)
          //--------------------------------------
          // Get the parent spread of this DOM object, or NULL.
          {
              var p = o && o.parent;
              if( (!p) || (p instanceof Document) ) return null;
              return ( (p instanceof Spread) || (p instanceof MasterSpread) ) ? p : F(p);
          };
          
          var getProxyPageName = function(/*PageItem*/o)
          //--------------------------------------
          // Get the name of the page that either contains this object, or
          // is at the shortest distance of this object along the x-axis.
          {
              const CS_SPREAD = +CoordinateSpaces.SPREAD_COORDINATES,
                    AP_CENTER = +AnchorPoint.centerAnchor,
                    mABS = Math.abs;
              
              var x,p,a,d,i,t,iBest;
          
              if( !(p=o.parentPage) && (p=parentSpread(o)) )
                  {
                  x = o.resolve(AP_CENTER,CS_SPREAD)[0][0];
                  for( d=1/0, a=p.pages.everyItem().getElements(), i=a.length ; i-- ; )
                      {
                      t = mABS(x - a[i].resolve(AP_CENTER,CS_SPREAD)[0][0]);
                      if( t < d ){ d=t; iBest=i; }
                      }
                  p = a[iBest];
                  }
              
              return p ? p.name : '';
          };
          
          // Test
          // ---
          var o = app.properties.selection && app.selection[0];
          if( o ) alert( getProxyPageName(o) );
          

           

          @+

          Marc

          • 2. Re: How to get corresponding page object for slug data?
            mmmathur Level 1

            Thanks a ton Marc!! This is exactly same, I was looking for.

             

            Mac