4 Replies Latest reply on Jul 5, 2013 1:12 AM by Gavin1985

    Page Number of a Search Result

    Gavin1985

      Hi,

       

      Can someone please help me with a problem that has got me pulling my hair out at the moment. I've Googled extensively, and had no joy. My Indesign Scripting experience is fairly low, but I spend a lot of time in VBA and PHP so coding and objects are not alien to me.

       

      I am modyfing this existing (and excellent) script to cover my needs.I need to search for each instance of a particular paragraph style, then use the text and page number to build a contents page later on. The original script acheived this via an index, however that is not convinient for me in this case as I need to do more modification first.

       

      I have succesfully converted the original script to search for all text that has a particular paragraph style on it. I am then succsefully grabbing the text. What I just can't seem to do, is grab the page number from the search result. I understand that I probably need to look at the tags parent page, but I can't see how. If it is of any help, the paragraph style is likely to be nested within two or three anchored tags in a text box.

       

      Any help would be greatly appreciated.

       

       

      // JavaScript Document

      /*

                Based on "Marking up index words from character style" by Josha Hubbers which was based on the script "Marking up index words from Color" by Thomas Silkjær

                http://indesigning.net/

      */

      var result_words_found = 0;

      var curr_document = 0;

      var my_document = app.documents.item(curr_document);

       

       

      // Create a list of swatches

      //var list_of_paraStyles = my_document.paragraphStyles.everyItem().name;

      var list_of_paraStyles = my_document.paragraphStyleGroups.item("Book").paragraphStyles.everyItem().name;

      //var list_of_paraStyles = my_document.ParagraphStyleGroup.allParagraphStyles()

      var yesNoDialog=["Yes","No"];

       

       

      // Make dialog box for selecting the char style to index

      var paraStyle_dialog = app.dialogs.add({name:"Generate index from Paragraph style"});

      with(paraStyle_dialog.dialogColumns.add()) {

                with(borderPanels.add()) {

                          with(dialogRows.add()) {

                                    with(dialogColumns.add()) {

                                              staticTexts.add({staticLabel:"Choose paragraph style:"});

                                    }

                                    with(dialogColumns.add()) {

                                              var selected_paraStyle = dropdowns.add({stringList:list_of_paraStyles, selectedIndex:0});

                                    }

                          }

                          with(dialogRows.add()) {

                                    with(dialogColumns.add()) {

                                              staticTexts.add({staticLabel:"Search all open documents:"});

                                    }

                                    with(dialogColumns.add()) {

                                              var selected_search_span = dropdowns.add({stringList:yesNoDialog, selectedIndex:1});

                                    }

                          }

                }

      }

       

       

      // catch the cancel button

      var doSearch = paraStyle_dialog.show();

       

       

      // if selected_search_span.selectedIndex == 0 then search "all documents".

      // else only search active document.

      // active document is item 0 in the app.documents.item array

       

       

      if (doSearch) {

                // Set find text options to find all words with the selected character style

                app.findChangeTextOptions.caseSensitive = false;

                app.findChangeTextOptions.wholeWord = false;

                app.findChangeTextOptions.includeFootnotes = false;

                app.findChangeTextOptions.includeHiddenLayers = false;

                app.findChangeTextOptions.includeLockedLayersForFind = false;

                app.findChangeTextOptions.includeLockedStoriesForFind = false;

                app.findChangeTextOptions.includeMasterPages = false;

       

                app.findTextPreferences = NothingEnum.nothing;

                app.findTextPreferences.appliedParagraphStyle = my_document.paragraphStyleGroups.item("Book").paragraphStyles.item(selected_paraStyle.sel ectedIndex);

       

                // loop through the open documents to index them all...

                do {

                          my_document = app.documents.item(curr_document);

                          // Search the document

                          var found_words =  my_document.findText();

       

                          // Make index

                          var my_contents

       

                          // check if words are found

                          if (found_words.length > 0){

       

                                    // Loop through the paragraphs, create a list of words and mark them as index words

                                    myCounter = found_words.length-1;

                                    do {

                                              var found_word = found_words[myCounter];

                                              var my_topic_name = found_word.contents;

                                              // check if topic is not empty

                                              if (my_topic_name.length > 1){

                                                        // items are found

                                                        result_words_found += 1;

       

       

       

       

      // Need to grab the page number as a variable here.

       

       

       

                                                        // Create the page reference

      //                                                  my_topic.pageReferences.add(found_word);

                                              }

                                              myCounter--;

                                    } while (myCounter >= 0);

                          }

       

                          // remove unused topics from the index, just for cleanup

       

                          // break if only current document is search scope

                          if (selected_search_span.selectedIndex == 1){

                                    break;

                          }

       

                          curr_document += 1;

                } while (curr_document < app.documents.count());

       

                // display result dialog...

                if (result_words_found == 0){

                          // alert nothing found

                          alert("Nothing was found, try again...");

                }else{

                          alert("Done indexing!\n"+result_words_found+" items found.\n\nNow choose \"Generate index\" in the Index panel to update your index.");

                }

      }

        • 1. Re: Page Number of a Search Result
          Peter Kahrel Adobe Community Professional & MVP

          The number of the page on which some text sits is the name of the text's parent text frame's parent page (if you see what I mean):

           

          var found_words =  my_document.findText();

          $.writeln (found_words[0].parentTextFrames[0].parentPage.name);

           

          That's CS5 and later. In CS4 it's slightly different, I forget the details.

           

          Peter

          • 2. Re: Page Number of a Search Result
            Gavin1985 Level 1

            Thats perfect, thanks Peter. For some reason my counter isn't working properly to get the correct page number for each line, but I should be able to nail that one myself.

            • 3. Re: Page Number of a Search Result
              Laubender Adobe Community Professional & MVP

              @Gavin – instead of the name property (which value might not be unique ! ), you could use the zero-based documentOffset property together with parentPage…

               

              $.writeln (found_words[0].parentTextFrames[0].parentPage.documentOffset);
              

               

              There are also "special cases", where, geometrically spoken, you'll get the wrong page:

               

              1. A table exceeds a page in double sided document:

               

              TableExceedsAPage.png

               

              2. Or here a text frame with its center on page 3:

               

              TextFrameCenteredOnPage3.png

               

              If you want the geometrical position of your text, you have to refine your method ;-)

               

              3. Also choose your sample of text wisely.

              A word could always have more than just one parent text frames ( a hyphenated word over one or more threaded text frames ).

               

              Also consider text on paths…

               

              Uwe

              1 person found this helpful
              • 4. Re: Page Number of a Search Result
                Gavin1985 Level 1

                Thanks for that Laubender.

                 

                I am creating my pages via a very strict template with only limited choices in this case, so it shouldn't be a problem. But i can forsee more and more projects like this one for me over the next year, so I will lodge this in the memory banks for the future.