14 Replies Latest reply on Mar 9, 2017 11:57 PM by Laubender

    How to search the contents of text frame anchored to the current story

    Christopher Pickering Level 1

      Hi,

       

      I have a multipage story with many anchored text frames. I'm trying to find a way to search the contents of all the anchored objects on my current page. I've only been able to do it by selecting all text frame objects on the current page and then search them. However, this include other text frames that may have a match I don't want.

       

      However I'm not sure how this is even working as when I run

      alert(app.activeWindow.activePage.pageItems.length)

      the number of items returned is all the text frames on the page except the anchored ones.. in this case, 6 instead of 21.

       

      Any ideas you have are appreciated!

       

      Thanks!
      Christopher

        • 2. Re: How to search the contents of text frame anchored to the current story
          Christopher Pickering Level 1

          Thanks, that will find the anchor point in the main story, but how can I get to the contents of the anchored text frame?

          • 3. Re: How to search the contents of text frame anchored to the current story
            Trevorׅ Adobe Community Professional

            You can actually us find text and use "^a"

            The use find = myFinds[n].textFrames[0] then contents = find.isValid && find.contents

            • 4. Re: How to search the contents of text frame anchored to the current story
              Laubender Adobe Community Professional & MVP

              Hi Christopher,

              if you want a search through formatted text of all anchored text frames of your main story, you could build an array of formatted text objects and search this with GREP or TEXT Find/Replace like that:

               

              1. Select some text or a text frame of your main story

              2. Loop through the parentStory.textFrames.everyItem().texts.everyItem().getElements() array.

              3. Now you could do a GREP or TEXT Find/Replace action on every instance found.

               

              var sel = app.selection[0];
              var mainStory = sel.parentStory;
              var fomattedTextsOfAnchoredFrames = mainStory.textFrames.everyItem().texts.everyItem().getElements();
              
              for(var n=0;n<fomattedTextsOfAnchoredFrames.length;n++)
              {
              doSomethingWithFormatted(fomattedTextsOfAnchoredFrames[n]);
              };
              
              // Build a function that will do something with the formatted text of a text frame
              
              function doSomethingWithFormatted(text)
              {
              // Do something reasonable here:
              // … A GREP Find/Replace action perhaps …
              
              // Not very reasonable, but maybe enlightening. 
              // Write the contents of the anchored text frames to the JavaScript Console of the ESTK
              $.writeln(text.contents)
              }
              

               

              Regards,
              Uwe

              1 person found this helpful
              • 5. Re: How to search the contents of text frame anchored to the current story
                Trevorׅ Adobe Community Professional

                Hi Uwe,

                 

                Nice one,

                 

                That's normally going to be a better approach than using the Grep.

                If one doesn't need to loop through the individual frames one could just do find / change on the whole collection.

                 

                var sel = app.selection[0];  
                var mainStory = sel.parentStory;  
                var fomattedTextsOfAnchoredFrames = mainStory.textFrames.everyItem().texts.everyItem();  
                fomattedTextsOfAnchoredFrames.fillColor = app.activeDocument.swatches[5];
                app.findTextPreferences.findWhat = 'd';
                app.changeTextPreferences.changeTo = 'D';
                fomattedTextsOfAnchoredFrames.changeText();
                

                 

                Regards

                 

                Trevor

                • 6. Re: How to search the contents of text frame anchored to the current story
                  Obi-wan Kenobi Adobe Community Professional

                  Hi Uwe & Trevor,

                   

                  Why mention the "main" story"?

                   

                  Is this not enough?

                   

                  var myDoc = app.activeDocument;
                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  app.findGrepPreferences.findWhat = "~a";
                  myFound = myDoc.findGrep();
                  var F = myFound.length;
                  for ( var f = 0; f < F; f++ ) if ( myFound[f].textFrames[0].isValid ) {
                      // Do something!
                      app.findChangeTextOptions.includeMasterPages = false;
                      app.findGrepPreferences = app.changeGrepPreferences = null;
                      //…………………
                      }
                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  

                   

                   

                  (^/) 

                  • 7. Re: How to search the contents of text frame anchored to the current story
                    Laubender Adobe Community Professional & MVP

                    Hi Obi-wan,

                    you would find perhaps too much. The scope with your code is the whole document.
                    You'll find also anchored objects with other stories that are not part of the "main story".

                     

                    Regards,
                    Uwe

                    • 8. Re: How to search the contents of text frame anchored to the current story
                      Obi-wan Kenobi Adobe Community Professional

                      … So, how define the "main" story? 

                       

                      (^/)

                      • 9. Re: How to search the contents of text frame anchored to the current story
                        Laubender Adobe Community Professional & MVP

                        Trevorׅ  wrote

                        … If one doesn't need to loop through the individual frames one could just do find / change on the whole collection. …

                         

                        Hi Trevor,

                        yes. But all depends on the structure of the anchored frames.
                        That could be imply deeper nested text frames or tables etc.pp.

                         

                        If there are only text frames anchored and no text path objects my idea will work.

                         

                        If there are groups anchored with text frames inside we need a different approach to gather all formatted text.
                        Also if one wants text in table cells where the table is placed in anchored frames…

                         

                        One could also start with:

                         

                        app.selection[0].parentStory.allPageItems;
                        

                         

                        and do a little sorting. Extracting tables with a TEXT search after <0016> getting to texts of the cells.
                        And finally get a flat array of text objects by concatenating every texts to a result array.

                         

                        Regards,
                        Uwe

                        • 10. Re: How to search the contents of text frame anchored to the current story
                          Laubender Adobe Community Professional & MVP

                          https://forums.adobe.com/people/Obi-wan+Kenobi  wrote

                           

                          … So, how define the "main" story? 

                           

                          (^/)

                          Only Christopher could answer that.

                          We cannot see in his document…

                           

                          After re-reading his first post, I'm not sure if there is only one main story.
                          Or if there are some threaded text frames going to other pages where he only is interested in the text frame(s) of one page.

                           

                          Before doing any other suggestions we should hear from him how his page is structured.

                           

                          Regards,
                          Uwe

                          • 11. Re: How to search the contents of text frame anchored to the current story
                            Christopher Pickering Level 1

                            Thanks!

                             

                            There are 6 text frames on the page, I'm only interested in one of them, which has the anchored text frames.

                             

                            Only the main frame I'm interested in is threaded text to other pages, but I only want to search the anchored objects on the current page

                             

                            Thanks!
                            Christopher

                            • 12. Re: How to search the contents of text frame anchored to the current story
                              Christopher Pickering Level 1

                              Thanks!

                              app.findGrepPreferences = app.changeGrepPreferences = null;  
                              app.findGrepPreferences.findWhat = "~a";  
                              myFound = myFrame.findGrep();  
                              var F = myFound.length;  
                              for ( var f = 0; f < F; f++ ) if ( myFound[f].textFrames[0].isValid ) {  
                                  // Do something!  
                                  //app.findChangeTextOptions.includeMasterPages = false;  
                                  //app.findGrepPreferences = app.changeGrepPreferences = null;  
                                  //…………………  
                              alert(myFound[f].textFrames[0].contents)
                              }  
                              }
                              

                               

                              Works perfectly for what I was trying to do. All the frames on the page have a variable name. The script is building a book from rtf files and doing the page layout and design one page at a time.

                               

                              However, for curiosity

                               

                              How would you search for a string in the anchored frames or a character style?

                               

                              For example, if I was searching for the string "I love indesign scripting" with character style "javascript_is_best" in one of the anchored frames to "myFrame"?

                              • 13. Re: How to search the contents of text frame anchored to the current story
                                Christopher Pickering Level 1

                                Got it! Maybe there is a cooler way but this works! I guess that is what some of you were trying to point out in the beginning...

                                 

                                var myDoc = app.activeDocument; 
                                app.findGrepPreferences = app.changeGrepPreferences = null; 
                                app.findGrepPreferences.findWhat = "~a"; 
                                myFound = myDoc.findGrep(); 
                                var F = myFound.length; 
                                for ( var f = 0; f < F; f++ ) if ( myFound[f].textFrames[0].isValid ) { 
                                app.findGrepPreferences.findWhat = "I love indesign scripting";
                                app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("javascript_is_best")
                                founds2 = myFound[f].textFrames[0].findGrep()
                                // I only care about the first one.. the others can wait
                                if(founds2[0]){alert(founds2[0].contents)}
                                app.findGrepPreferences = app.changeGrepPreferences = null; 
                                } 
                                
                                • 14. Re: How to search the contents of text frame anchored to the current story
                                  Laubender Adobe Community Professional & MVP

                                  Hi Christopher,

                                  no. If you are looking at your code, you are not there.

                                   

                                  You are writing:

                                  I have a multipage story with many anchored text frames. I'm trying to find a way to search the contents of all the anchored objects on my current page.

                                   

                                  1. multipage story

                                  2. many anchored text frames

                                   

                                  You want to know:

                                   

                                  1. anchored objects

                                  2. Current page

                                   

                                  Your code is:

                                  myFound = myDoc.findGrep()

                                   

                                  That would also find anchored frames not on the current page.
                                  It would even find anchored objects on the pasteboard. Or on master spreads.

                                   

                                  app.documents[0].layoutWindows[0].activePage.findGrep();
                                  

                                   

                                  is no option. findGrep() is a property of: app, cell, character, column, document, insertionPoint, line, paragraph, row, story, table, text, textColumn, textFrame, textPath, textStyleRange, word, XMLElement, XmlStory

                                   

                                  We have to look into a different method to identify all anchored text frames in your story on the current page.

                                   

                                  I don't know how you identify your multipage story.

                                  Obi-wan is asking this as well.

                                   

                                  Let's assume you have identified it somehow.

                                  You first have to find out what text containers of your story are on the current page.

                                   

                                  The snippet below is not my favorite. It only works best if we do some assumptions. It would fail if you e.g. anchored groups with text frames.
                                  And it assumes that all anchored text frames on the "current" page belong to your story you are interested in.

                                   

                                  But it will find the anchored text frames on your current page only.

                                   

                                  var myDoc = app.activeDocument;
                                  var currentPage = myDoc.layoutWindows[0].activePage;
                                  
                                  app.findGrepPreferences = app.changeGrepPreferences = null;   
                                  app.findGrepPreferences.findWhat = "~a";   
                                  myFound = myDoc.findGrep();
                                  
                                  var F = myFound.length;
                                  for ( var f = 0; f < F; f++ )
                                  {
                                      if
                                      (
                                          // We are interested in text frames only. 
                                          // Not anchored graphic frames etc.pp.
                                          myFound[f].textFrames[0].isValid && 
                                          // Is not on the pasteboard:
                                          myFound[f].textFrames[0].parentPage != null && 
                                          // Belongs to the "current" page; I assume you mean the activePage :
                                          myFound[f].textFrames[0].parentPage.documentOffset == currentPage.documentOffset
                                      )
                                      {
                                          $.writeln(f+"\t"+myFound[f].textFrames[0].contents);
                                      }
                                  
                                  };
                                  

                                   

                                  Also test:

                                   

                                  $.writeln(f+"\t"+myFound[f].textFrames[0].contents);
                                  
                                  // vs.
                                  
                                  $.writeln(f+"\t"+myFound[f].textFrames[0].parentStory.texts[0].contents);
                                  

                                   

                                  if one of the text frames is in overset.

                                   

                                  Not provided: text in tables, nested tables, footnotes, notes.

                                   

                                  Regards,
                                  Uwe

                                   

                                  // EDITED: Somehow the forum software messed up my formatting
                                  // Hope, this is correct now.