Skip navigation
Frillo@Adobe
Currently Being Moderated

Paste found text in new frame

Mar 23, 2012 4:38 AM

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

 
Replies
  • Currently Being Moderated
    Mar 23, 2012 5:37 AM   in reply to Frillo@Adobe

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 24, 2012 11:33 AM   in reply to Frillo@Adobe

    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".

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 26, 2012 6:59 AM   in reply to Frillo@Adobe

    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..?".)

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 18, 2012 10:13 AM   in reply to Frillo@Adobe

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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 18, 2012 11:58 AM   in reply to Frillo@Adobe

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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 19, 2012 4:33 AM   in reply to Frillo@Adobe

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 19, 2012 6:59 AM   in reply to Frillo@Adobe

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 19, 2012 8:33 AM   in reply to Frillo@Adobe

    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).

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 5:57 AM   in reply to Frillo@Adobe

    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).

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points