2 Replies Latest reply on Mar 10, 2011 1:55 PM by [Jongware]-9BC6tI

    page number (search)

    jeanmed Level 1

      Hello

       

      I want to find the page af text that i find with the finText methode.

       

      Can you help me to find "myPageNumber " (see my code).

       

      Thanks

       

       

      var myDocument = app.documents.item(0);
      app.findTextPreferences = app.changeTextPreferences = null;
      app.findTextPreferences.appliedCharacterStyle = "test";
      var myResults = app.activeDocument.findText();
      for (f = 0; f < myResults.length; f++)
      {
      var bm_name= myResults[f].contents;
      var myPageNumber =?????;
      alert(myPageNumbe);
      }

        • 1. Re: page number (search)
          Mi_D Level 3

          Dear jeanmed,

           

           

          var myDocument = app.documents.item(0);

          var myPage = myDocument.pages;

          app.findTextPreferences = app.changeTextPreferences = null;

          app.findTextPreferences.appliedCharacterStyle = "test";

          var myResults = app.activeDocument.findText();

          for (f = 0; f < myResults.length; f++)

          {

           

          myResults[f].select();

          var myPg = myPage[f];

          var frame = app.selection[0].parentTextFrames;

           

          var myPg = myPage.everyItem();

          var myPagenumber = myPg.name;

           

          }

          alert ("test style is used in : page number" +myPagenumber )

           

          Mi_D

          • 2. Re: page number (search)
            [Jongware]-9BC6tI Level 4

            There are a number of fundamental errors in your script, but the very first one is you don't mention your version of InDesign! That's important, because with CS5 you can easily get the page of about every single object, and if you have the page, you can read the page number (this property is conveniently called the object's "parentPage"). With CS4 and older, you have to check the parent of your text object, and its parent in turn, etc., until you find the actual page -- or something else (f'r instance, if the object was on the pasteboard instead).

             

            Another error was in checking myResults[f].contents -- the "contents" of a found object is just a plain text string, and you cannot find the parent page of a plain text string. Just check the returned object immediately. I find it useful to check just the first character, by the way, because your text style could continue from one page onto another, and what page should be reported then?

             

            It's not very useful to pop up an alert for each separate found instance; I store the result into an array and show that after all results are gathered.

             

            var myDocument = app.documents.item(0);
            app.findTextPreferences = null;
            app.findTextPreferences.appliedCharacterStyle = "test";
            var myResults = app.activeDocument.findText();
            
            var resultList = [];
            
            for (f = 0; f < myResults.length; f++)
            {
            //     var myPageNumber = getPageFor(myResults[f]);
            //     alert (myPageNumber.name);
                 var r = getPageFor(myResults[f].characters[0]);
                 resultList.push ( r.name);
            }
            alert (resultList.join (", "));
            
            function getPageFor (element)
            {
                 while (!(element instanceof Page || element instanceof Application))
                      if (element instanceof Character)
                           element = element.parentTextFrames[0].parent;
                      else
                           element = element.parent;
                 if (element instanceof Page)
                      return element;
                 return null;
            }