19 Replies Latest reply: Apr 20, 2012 5:57 AM by [Jongware] RSS

    Paste found text in new frame

    Frillo@Adobe

      I have this script to find my desired text:

       

      var myDoc = app.activeDocument;

      // Creates  service Text Frame

      myDoc.textFrames.add({

          geometricBounds: [50,-300,740,-70], fillColor: "C=100 M=0 Y=0 K=0", fillTint: 20, name: "Summary"

          });

       

       

      //Find  text

      app.findGrepPreferences = null;

      app.findGrepPreferences.findWhat = "(.+\\r)(\\+.+$)";

      found = app.activeDocument.findGrep ();

      alert (found.length)

      var new_grep = new Array()

      for(a=0; a<found.length; a++)

      {

      new_grep[a] = found[a].contents

      }

       

       

      Now, what I'd like to know is how to paste the found text (the contents of new_grep) into my textFrame "Summary".

      Additionally, I should erase substring #2 (\\+.+$) from the original frame...

       

      Trying and trying but I can't find my way, I know, it should be damn simple...

       

      Thanks,

      Guido

        • 1. Re: Paste found text in new frame
          [Jongware] CommunityMVP

          After grabbing the contents, use the same GREP again on your document and replace with nothing.

          Then put the copy of the found items into a frame like this:

           

          summary_textframe.contents = new_grep.join("\r");

           

          The other way around will not work because you act on 'the entire document', and you would erase it from your new frame as well.

          • 2. Re: Paste found text in new frame
            Frillo@Adobe Community Member

            I solved by myself with a FAR LESS elegant method:

             

            var myDoc = app.activeDocument;

             

            . . .

             

            app.findGrepPreferences = null;

            app.findGrepPreferences.findWhat = "(.+\\r)(\\r\\+.+$\\r?)";

            myFind = app.activeDocument.findGrep ();

            var myCount=0;

            for (i=0; i<myFind.length; i++)

            {

            myDoc.textFrames.item ("Summary").contents=myDoc.textFrames.item ("Summary").contents+myFind[i].contents+"\r";

            myCount++;

            }

             

            Concerning the second part of the problem (erasing substring #2 from the main frame) I will simply perform a second find/change routine replacing the substring #2 with ()...

            Is this correct?

            Thank you, Jongware

            Guido

            • 3. Re: Paste found text in new frame
              [Jongware] CommunityMVP

              Frillo wrote:

              Is this correct?

               

              Sure. If you search for (a)(b) and you want to keep (a) and loose (b), just replace with "$1".

              • 4. Re: Paste found text in new frame
                Frillo@Adobe Community Member

                Very last thing: is it possible to apply a Paragraph Style that applies next style (and this applies next, and next...)?

                I have 4 distinct Styles to be applied in a loop (Style 1 > 2 > 3 > 4 > 1 > 2 >....etc)

                Thank you for your invaluable support, Jongware,

                g

                • 5. Re: Paste found text in new frame
                  [Jongware] CommunityMVP

                  You can set this up in the Next Paragraph style field per style. Then all you have to do is select all text and "Apply "This" style, then Next" from the Paragraph style menu. Way more easier than scripting it. (*)

                   

                  (*) Not really, but it would be a script that duplicates something that's already available. I'm not falling for that.(**)

                   

                  (**) Again. (I once wrote a large script and people wrote comments like "uh, isn' that just like command 'X' in the interface..?".)

                  • 6. Re: Paste found text in new frame
                    Frillo@Adobe Community Member

                    Hi Jongware,

                    after a bit I returned to my script and, of course... it doesn't work anymore!

                     

                    This is what I'm up to:

                     

                    var myDoc = app.activeDocument;

                    // Creates  service Text Frame

                    myDoc.textFrames.add({

                        geometricBounds: [50,-300,740,-70], fillColor: "C=100 M=0 Y=0 K=0", fillTint: 20, name: "Summary"

                        });

                     

                     

                    //Find text

                    app.findGrepPreferences = null;

                    app.findGrepPreferences.findWhat = "(.+\\r)(\\r\\+.+$ \\r)";

                    myFind = app.activeDocument.findGrep ();

                    var myCount=0;

                    for (i=0; i<myFind.length; i++)

                    alert (myFind)

                    {

                    myDoc.textFrames.item ("Summary").contents=myDoc.textFrames.item ("Summary").contents+myFind[i].contents+"\r";

                    myCount++;

                    }

                    // Delete "+ foto"

                    app.findGrepPreferences = null;

                    app.findGrepPreferences.findWhat = "(\\r\\+ FOTO.+$)";

                    myDoc.selection[0].changeGrep();

                     

                     

                    The new frame is created, but nothing gets pasted in it.

                     

                    I DO REMEMBER it used to work (that was before my hard disk crashed and I had to open my iMac to replace it...), God knows what I've done wrong.

                    Suggestions?

                     

                    Thx,

                    g

                    • 7. Re: Paste found text in new frame
                      [Jongware] CommunityMVP

                      You have to be more specific. "It doesn't work" doesn't tell me much.

                      • 8. Re: Paste found text in new frame
                        Frillo@Adobe Community Member

                         

                        Sorry, as I said, the new text frame is created but nothing goes in it.

                        I don't know how to find out if the array itself gets populated with the grep find results; is there anyway to check the variables/arrays values in the ESTK?

                        Thx for the quick answer...

                        g

                        • 9. Re: Paste found text in new frame
                          [Jongware] CommunityMVP

                          Use the ESTK debugger, as described in today's thread http://forums.adobe.com/thread/991744?tstart=0

                          • 10. Re: Paste found text in new frame
                            Frillo@Adobe Community Member

                            Ok, sorted out.

                            There was an error in the find query; it was:

                             

                                 app.findGrepPreferences.findWhat = "(.+\\r)(\\r)(\\+.+$ )\\r)";

                             

                            There was a space before the last \r entry, so I solved with

                             

                                 app.findGrepPreferences.findWhat = "(.+\\r)(\\r)(\\+.+$( ?)\\r)";

                             

                            because sometimes THERE IS a trailing space at the end of the paragraph.

                             

                            Now, in order to paste only subpatterns #1 and #3 in the new frame, should I do:

                             

                            //Find  text

                            app.findGrepPreferences = app.changeGrepPreferences = null;

                            app.findGrepPreferences.findWhat = "(.+\\r)(\\r)(\\+.+$( ?)\\r)";

                            app.activeDocument.findGrep

                            app.changeGrepPreferences.changeTo = "$1\\r$3"

                            myFind = app.activeDocument.findGrep ();

                             

                            ...maybe?

                            • 11. Re: Paste found text in new frame
                              [Jongware] CommunityMVP

                              The subpattern replacement and changeTo property only work with changeGrep, which means that you would change them in your actual document.

                               

                              The only way to get what you want is to do your findGrep, then make a new array containing just the text (because the result from findGrep points directly to your actual live text) -- your original array "new_grep" method in your first post is fine.  In this copy, you can use regular Javascript string functions to re-arrange the text to your like. After that you can paste the processed text into a new frame.

                               

                              For example, to get rid of the single space at the end of a paragraph in Javascript, you could do something like this:

                               

                              new_grep[i] = new_grep[i].replace (/ \r/g, '\r');

                               

                              -- if there is nothing to replace, the string will be unchanged.

                              • 12. Re: Paste found text in new frame
                                Frillo@Adobe Community Member

                                Sorry Jongware,

                                I don't think I get your last post.

                                What does (/ \r/g, '\r') mean, exactly..?

                                • 13. Re: Paste found text in new frame
                                  [Jongware] CommunityMVP

                                  It's the Javascript version of replacing your space/return with a single return. You need a Javascript version because InDesign's findGrep and changeGrep only work on InDesign text, not on Javascript strings.

                                  • 14. Re: Paste found text in new frame
                                    Frillo@Adobe Community Member

                                    Ok. And where can I find a reference to these Javascript versions? I'm still after a way to apply only found text 1 and 3, stripping out found text 2...

                                    You're very patient, thank you...

                                    g

                                    • 15. Re: Paste found text in new frame
                                      [Jongware] CommunityMVP

                                      Uh, Google? I'm not aware of any specific sites ...

                                       

                                      I think I learned all of my Javascript GREP knowledge from this very forum, precisely this same way you encountered it just now: someone using these expressions in an otherwise unrelated script. I'm sure there is nothing in Adobe's JS guides about this particular subject.

                                       

                                      In any case, you must certainly not include "indesign" in your google query. InDesign's own GREP is very different from Javascript's (a subject leading to much confusion).

                                      • 16. Re: Paste found text in new frame
                                        Frillo@Adobe Community Member

                                        Found something, thank you. The problem was I was googling with "InDesign" in the query, as you said...

                                         

                                        Anyway, now my simple script:

                                         

                                        var myDoc = app.activeDocument;

                                        // Creates  service Text Frame

                                        myDoc.textFrames.add({

                                            geometricBounds: [50,-300,740,-70], fillColor: "C=100 M=0 Y=0 K=0", fillTint: 20, name: "Summary"

                                            });

                                         

                                         

                                        //Find  text

                                        app.findGrepPreferences = null;

                                        app.findGrepPreferences.findWhat = "(.+\\r)(\\r)((\\+.+$( ?)\\r)+)";

                                        found = app.activeDocument.findGrep ();

                                        var new_grep = new Array()

                                        for(a=0; a<found.length; a++)

                                        new_grep[a] = found[a].contents;

                                        var Summary = myDoc.textFrames.item ("Summary")

                                        Summary.contents = new_grep.join("\r");

                                         

                                        Stops at line 3, - myDoc.textFrames.add - saying "undefined is not an object... I can't think nothing more vague than this...

                                        g

                                        • 17. Re: Paste found text in new frame
                                          Frillo@Adobe Community Member

                                          Self reply:

                                          I didn't assign InDesign as the destination app...

                                          ...Shame on me...

                                           

                                          • 18. Re: Paste found text in new frame
                                            Frillo@Adobe Community Member

                                            So, /g means "Apply this globally"

                                             

                                            But I'm having other difficulties applying your line

                                             

                                            new_grep[i] = new_grep[i].replace (my find/replace query here);

                                             

                                            in my script.

                                             

                                            If I write it that way, ESTK responds with "new_grep.replace is not a function" (I guess the problem is in the parenthesis).

                                            g

                                            • 19. Re: Paste found text in new frame
                                              [Jongware] CommunityMVP

                                              Does your new_grep array contain plain text, as it did in your initial post? I.e., are you filling it with the result of your GREP search as you did?

                                               

                                              for(a=0; a<found.length; a++)

                                              {

                                              new_grep[a] = found[a].contents;

                                              }

                                               

                                              The .replace function only works on Javascript strings. Copying 'contents' to a new array makes sure it gets translated to a JS string, and you cannot use it on a native InDesign Text object (which is the kind that findGrep itself returns).