5 Replies Latest reply on Mar 29, 2010 2:44 AM by Roy Marshall

    [JS][CS3] Getting Page Number

    Roy Marshall Level 1

      Hi.

       

      I need to find the page number of the page containing a frame.

      Normally I would use "myFrame.parent.name" to find the page number. However, I cannot use this method on an inline graphic.  What I get returned is the story, which has no page number.  I have used various ways, such as:

      myFrame.parent.insertionPoints[0].parent

      but nothing returns the page number.  I have looked at all the properties of the frame, and cannot find a property I can manipulate to extract the page number.
      Is this possible? Should I give up now?
      Cheers
      Roy

        • 1. Re: [JS][CS3] Getting Page Number
          கற்பனை (Imagine) Level 3

          Use the Below code,

           

          myFrame.parent.insertionPoints[0].parentTextFrames[0].parent.name

           

          Regards,

          Imagine.

          • 2. Re: [JS][CS3] Getting Page Number
            John Hawkinson Level 5

            Or simply myFrame.parent.parentTextFrames[0].parent.name.

            At least for me, the parent of an anchored object is a Character, which has a parentTextFrame that has a parent Page.

            • 3. Re: [JS][CS3] Getting Page Number
              Kasyan Servetsky Level 5

              Here is a function written by Dave Saunders:

              var myDoc = app.activeDocument;
              var myObj = app.selection[0];
              $.write("The selected object is on page " + findPage(myObj).name + "\n") ;
              
              function findPage(theObj) { 
                  var thePage = theObj; 
                  if (thePage.hasOwnProperty("baseline")) { 
                    thePage = thePage.parentTextFrames[0]; 
                  } 
                  while (thePage.constructor.name != "Page") { 
                    var whatIsIt = thePage.constructor.name; 
                    switch (whatIsIt) { 
                      case "Story" : 
                        thePage = thePage.textFrames[-1].parent; 
                        break; 
                      case  "Character" : 
                        thePage = thePage.parentTextFrames[0]; 
                        break; 
                      case "Cell" : 
                        try { 
                          thePage = thePage.insertionPoints[0].parentTextFrames[0]; 
                          break; 
                        } catch (e) { 
                          // must be overset, so ignore 
                          return null; 
                        } 
                      case "Application" : 
                        // must be off page, so ignore 
                        return null; 
                    } 
                    thePage = thePage.parent; 
                  } 
                  return thePage 
                }
              
              • 4. Re: [JS][CS3] Getting Page Number
                Kasyan Servetsky Level 5

                Another function by Marc Autret

                // by Marc Autret
                TextFrame.prototype.getPage = function()
                // -----------------------------------------------
                // Returns the containing Page of this TextFrame
                {
                var p = this.parent,
                     pc;
                while( pc=p.constructor )
                     {
                     if( pc == Page )
                          return p;
                     if( pc == Spread || pc == MasterSpread )
                          throw Error("The textframe is placed on a spread!");
                     if( 'parentTextFrames' in p && !(p=p.parentTextFrames[0]) )
                          throw Error("The textframe's container overflows!");
                     p=p.parent;
                     }
                }
                 
                // Sample code (assuming a text frame is selected)
                // -----------------------------------------------
                var tf = app.selection[0],
                     pg;
                     
                try {
                     pg = tf.getPage();
                     alert( "Page: " + pg.name + " Side: " + pg.side);
                     }
                catch(e)
                     {
                     alert( e );
                     }
                
                • 5. Re: [JS][CS3] Getting Page Number
                  Roy Marshall Level 1

                  Wow, thanks all.  The first answer is what I have used, although will concider stripping it back as the second.  I am not experienced enough too understand all that Daves script is doing, but will look at it for helpful tips.

                   

                  Thanks all again

                   

                  Roy