7 Replies Latest reply on Apr 23, 2014 12:03 AM by Trevorׅ

    how to recognize page of a selected item

    roberto_zuc

      Hello to all

       

       

      I would like to know how to recognize page of a selected item in javascript

       

       

      thanks

        • 1. Re: how to recognize page of a selected item
          Sajeev Sridharan Level 4

          alert(app.selection[0].parentPage.name) // this will give the page number of the selected object

          app.selection[0].parentPage.select() // this will select the page of the corresponding object

           

           

          Vandy

          • 2. Re: how to recognize page of a selected item
            Trevorׅ Adobe Community Professional

            Hi Vandy and Roberto

             

            Often better to use documentOffset rather than name

            app.selection[0].parentPage.documentOffset

            If the selection is not a page item then you have to traverse up until you get to a page item.

            For example if you have text in a textFrame selected you would use

            app.selection[0].parentTextFrames[0].parentPage.documentOffset.

             

            If the selection is somewere nested like in a cell of a table which is in a cellof a table you can use this type of function

             

             

            function getPage(po) {
                po = app.selection[0] && app.selection[0];
                if (!po) return -2 // no selection so quit
                while (!po.hasOwnProperty("parentPage")) po = po.parent;
                if (po.parentPage == null) return -1; // off the page;
                return po.parentPage.documentOffset; // or if you want to return the actual page then just use return po.parentPage
            }
            
            alert (getPage());
            

             

            Trevor

            1 person found this helpful
            • 3. Re: how to recognize page of a selected item
              roberto_zuc Level 1

              Thanks to all

               

               

              with your help I was able to improve the functioning of one of my scripts, and I have improved my knowledge.

               

               

              Roberto

              • 4. Re: how to recognize page of a selected item
                banl_05 Level 1

                Hi Vandy

                 

                alert(app.selection[0].parentPage.name)

                 

                works for cs5 and above only.

                 

                Can you please help me for CS4

                 

                Thanks

                Balaji

                • 5. Re: how to recognize page of a selected item
                  Jump_Over Level 5

                  Hi,

                   

                  Take a solution here:

                  http://forums.adobe.com/thread/615381

                   

                  post#4 Dave's findPage(theObj) function

                   

                  Jarek

                  • 6. Re: how to recognize page of a selected item
                    banl_05 Level 1

                    Thanks Jump_Over

                     

                    Thanks,

                    Balaji

                    • 7. Re: how to recognize page of a selected item
                      Trevorׅ Adobe Community Professional

                      Hi All,

                       

                      Regarding pre CS5 findPage function

                       

                      I tried out Dave's script (Jareks link in 5 above).  After removing the cs5 line below so I could simulate having cs4, it would not work for me.

                      if (theObj.hasOwnProperty ("parentPage")) return theObj.parentPage;

                       

                      Marc also has  a find FindPage function see http://forums.adobe.com/message/2525480#2525480 and http://www.indiscripts.com/post/2011/03/indesign-scripting-forum-25-sticky-posts, his method also would not work for me

                       

                      I put together the following method (one in a function for and one in a prototype form) which works for me

                      Interesting to see 3 completly different approaches to the problem.

                       

                      The function form

                       

                      // Get's parentPage For pre CS5
                      function getParentPage (myThingamajig, myThingamajigID) {
                          myThingamajig = myThingamajig || app.selection[0] && app.selection[0]; // Your Item or text of the selection
                          if (!myThingamajig) return null; // none of the above so quit
                          while (!(myThingamajig instanceof Spread)) {
                              if (myThingamajig.hasOwnProperty ('baseline')) {
                                  myThingamajig = myThingamajig.parentTextFrames[0]
                              myThingamajigID = myThingamajig.id; // get the first parent object ID to find compare with pageItems IDs
                              }
                              else {
                              myThingamajigID = myThingamajig.id; // get the first parent object ID to find compare with pageItems IDs
                                  myThingamajig = myThingamajig.parent;
                              }
                              if (myThingamajig instanceof Application) return null; // don't think we sould need this ?-) 
                          }
                          
                          var np = myThingamajig.pages.length;
                          while (np--) {
                              if (("q" + myThingamajig.pages[np].pageItems.everyItem().id.join("q") + "q").match ("q" + myThingamajigID + "q")) return myThingamajig.pages[np]; // Item on the page so the page will be returned
                          }
                          return null;   // Item is on a Spread but not a Page
                          // Could use return myThingamajig; to Return the Spread
                      }
                      
                      // Show the Info
                      var p = getParentPage();
                      alert (p ? p.name : "Off the page or no selection");
                      
                      

                       

                       

                      In Prototype form (see government health warnings)

                       

                      // Get's parentPage For pre CS5 and also retrieves parentPage for characters etc. for all Indesing Versions
                      // By Trevor http://forums.adobe.com/thread/1445011?tstart=0
                      // A word of Warning for using from Harbs on using Object.prototype http://forums.adobe.com/message/3079012#3079012
                      Object.prototype.ParentPage = 
                      function  (override /* if true will use function for cs5+ else will revert to parentPage */) {
                          if (!override && parseInt(app.version) > 4) return this.parentPage;
                          var myThingamajig, myThingamajigID;
                          myThingamajig = this; // Your Item or text of the selection
                          while (!(myThingamajig instanceof Spread)) {
                              if (myThingamajig.hasOwnProperty ('baseline')) {
                                  myThingamajig = myThingamajig.parentTextFrames[0]
                              myThingamajigID = myThingamajig.id; // get the first parent object ID to find compare with pageItems IDs
                              }
                              else {
                              myThingamajigID = myThingamajig.id; // get the first parent object ID to find compare with pageItems IDs
                                  myThingamajig = myThingamajig.parent;
                              }
                              if (myThingamajig instanceof Application) return null; // don't think we sould need this ?-) 
                          }
                          
                          var np = myThingamajig.pages.length;
                          while (np--) {
                              if (("q" + myThingamajig.pages[np].pageItems.everyItem().id.join("q") + "q").match ("q" + myThingamajigID + "q")) return myThingamajig.pages[np]; // Item on the page so the page will be returned
                          }
                          return null;   // Item is on a Spread but not a Page
                          // Could use return myThingamajig; to Return the Spread
                      }
                      
                      
                      
                      // ********** Show the Info **********
                      
                      
                      var p = app.selection[0].ParentPage(true); 
                              /* 
                                   If left blank then from CS5 up the regular parentPage method will be used
                                   The regular parentPage method is quicker but will fail if the Object is a Character etc. 
                                   i.e. app.selection[0].characters[0].name etc. will fail and throw an error :-(
                                   
                                   If set to true then the prototype method will be used even on CS5+
                                   This ParentPage prototype will also work on characters etc. 
                                   i.e. app.selection[0].characters[0].name etc. will work and show the ParentPage name :-)
                               */
                      alert (p ? p.name : "Off the page or no selection");
                      
                      

                       

                       

                      Trevor