Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

GREP nested search script

Return to Member Forum

  • Author
    Posts
    • #1198963

      Newbie alert! I’ve only just finished the LinkedIn Learning course on ExtendScript for InDesign… I have successfully written my first useful custom script.

      Now I’m moving onto something a bit more tricky and I’m hoping someone can set me on the right path.

      (A couple of months ago I asked here if this could be done with a GREP style – and found out it couldn’t. I figure I can do almost anything with a script and perseverance!)

      The script must search out the name of a baby, and style it based on the tag “BOY” or “GIRL” a couple of paragraphs earlier.

      My earlier lesson from this forum was that that you can’t GREP search over multiple paragraphs, looking forward/backward – otherwise I could solve this by doing something like: ^BOY$^Photo: .+$^.+$
      (Since the story format looks like the following – with several babies listed in succession)

      ——
      GIRL
      Photo: 7054-57
      Harlo Maree SUTTON
      Born 14/10/19
      Weight 2990g
      Parents Teagan Sutton and Bobby Jeffery
      Siblings Kobie (9yrs), Jaxon (5yrs)
      Grandparents Donna Curtis, Tony Fuller, Brad Sutton
      ——

      So, I want to search through a story with GREP and look for either “BOY” or “GIRL” at the beginning of a paragraph. (i.e. ^BOY$ or ^GIRL$)
      At this point, I’d set a variable, for example: var baby=$1;

      THEN we go into the second search, which is looking for the “Photo:” paragraph. I guess at this point, the script would know that the next paragraph (Baby’s name) is to be styled using either the BOY or GIRL character style. (How to select it and apply the character style before moving on is another question)

      How do I do this? Do I somehow forloop through each paragraph in the story, checking each paragraph against my two main GREP styles and filling the baby variable with the appropriate sex each time I make a match?

      Or is there a much more efficient and sensible way?

      Happy to do the hard yards and read some tutes or sample scripts if someone can point me in the right direction.

      Thanks so much!

    • #14323507
      Tamás Nagy
      Participant

      Sorry for the off but why can’t I place my posts here?
      I posted yesterda, I posted today but after clicking Submit, the post just diappeared.

    • #14323503
      Tamás Nagy
      Participant

      Hi Brett,

      I wouldn’t bring InDesign’s text/grep search into it, I would simply go through all the paragraphs, using a status variable that stores which sex was found last. This status may directly point at the necessary character style. If the text you have to process is surely consists of records you showed above than a quite simple for loop will do the job. I mean if each and every record surely contains a sex field with exactly “GIRL” or “BOY” in it and anoter field that starts with “Photo:” and right after it comes the Name field then here’s a code yo can try. You should replace “…”s with the actual character style names.

      —————————————————

      var myD=app.activeDocument;
      var myBoystyle = myD.characterStyles.itemByName(“…”);
      var myGirlstyle = myD.characterStyles.itemByName(“…”);
      var myParags = myD.stories.everyItem().paragraphs.everyItem().getElements();
      var myStyle, nextP;

      for ( var p=0, plen=myParag.length; p<plen; p++ ) {

      if (myParags[p].contents==”BOY”) myStyle = myBoystyle;
      if (myParags[p].contents==”GIRL”) myStyle = myGirlstyle;

      nextP = myParags[p+1];

      // if there does exist a next paragraph AND this is the Photo field AND myStyle is alread defined…
      if ( nextP.isValid && nextP.contents.slice(0,6)==”Photo:” && myStyle ) nextP.appliedCharacterStyle = myStyle;

      };

      —————————————————

      I think it should work. But this code goes through ALL stories in the active document and it doesn’t recognise the story boundaries, so if “BOY” was found in a given story and “Photo:” in the next, it won’t make the difference, it will format the next paragraph of the second story. If you don’t like it, you have to make a double for cycle:

      —————————————————

      var myD=app.activeDocument;
      var myBoystyle = myD.characterStyles.itemByName(“…”);
      var myGirlstyle = myD.characterStyles.itemByName(“…”);
      var myStyle, nextP, myParags;

      var myStories=myD.stories;

      for ( var s=0, slen=myStroies.length; s<slen; s++ ) {

      myParags=myStories[s].paragraphs;
      myStyle=null;

      for ( var p=0, plen=myParags.length; p<plen; p++) {

      if (myParags[p].contents==”BOY”) myStyle = myBoystyle;
      if (myParags[p].contents==”GIRL”) myStyle = myGirlstyle;

      nextP = myParags[p+1];

      if ( nextP.isValid && nextP.contents.slice(0,6)==”Photo:” && myStyle ) nextP.appliedCharacterStyle = myStyle;

      }; // end for p

      }; // end for s

      —————————————————

      Notice: applying character styles on whole paragraphs is not too elegant, so I suggest to use paragraph styles instead.

      I hope, it helps, best,
      Tamás

    • #14323499
      David Blatner
      Keymaster

      Sorry about that… it was held for moderation for some reason. Cleared now.

    • #14323496
      Jeremy Howard
      Participant

      Hello Brett, I’m not sure if you saw my response to your previous post on this topic. I posted a solution there that involved using soft returns to separate your lines instead of using paragraph returns.

      Using that solution would allow you to use a nested GREP style and you wouldn’t need to run a find/change or a script of any kind. The styling would happen automatically! :)

Viewing 4 reply threads
  • You must be logged in to reply to this topic.
Forum Ads