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