Skip to main content
EmOlivette
Known Participant
July 28, 2021
Answered

GREP to remove runts in a contact listing within a paragraph

  • July 28, 2021
  • 1 reply
  • 2199 views

In my job, we create basically a phone book. Each entry is a paragraph, but each field of data is separated by a forced line break. If a company name is too long in the column, it will create a runt/cliff withing the boundary of the beginning of the paragraph to the end of the field of data (the forced line break). Is there a GREP I can run to identify when these happen? Note that I want to identify these, NOT do a change all. 

 

In theory what I want is to search for a single word on a line that has a forced line break at the end, but has no "beginning" other than it being on a second line. 

 

Does that make sense?

This topic has been closed for replies.
Correct answer FRIdNGE

In All reality, I don't know if a script can do a single find/change find/change like we can with the Find/Replace funtion. But If I can at least identify these runts, that would be most helpful


Just For Fun!

 

(^/)  The Jedi

 

 

/*
    _FRIdNGE-0728_LineRunt.jsx
    by FRIdNGE, Michel Allio [30/07/2021]
*/

var myDoc = app.activeDocument;

var myCondition = myDoc.conditions.item("runt");
if (!myCondition.isValid) myCondition = myDoc.conditions.add ({name: "runt", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT, indicatorColor: [255,186,131]});

var myStories = myDoc.stories,
S = myStories.length,  s;
for ( s = 0; s < S ; s++ ) {
    var myParas = myStories[s].paragraphs,
    P = myParas.length,  p;
    for ( p = 0; p < P ; p++ ) {
        var myLines = myParas[p].lines,
        L = myLines.length,  l;
        for ( l = 0; l < L-1 ; l++ ) if ( myLines[l].characters[-1].contents === " " ) myLines[l+1].appliedConditions = [myCondition];
    }
}

1 reply

Joel Cherney
Community Expert
Community Expert
July 28, 2021

Well, I'm not completely certain that I understand exactly what you're not looking for, but I think that this will find what you are looking for:

\S+\n(?-m)

This will find any non-spaces followed immediately by a forced line break. I wrote this regex to look for non-spaces,  but you may have to use something else, if you have single-word entries that you don't want your regex to find. 

 

I did this by adding "multiline off":

(?-m)

This regex will not find anything other than that single word. If there is a trailing space after the word and before the forced line break, this won't match it. 

EmOlivette
Known Participant
July 28, 2021

Think of it more so as: 

 

^p

ABC Welding & Supply^n
Jane Doe^n

etc. etc. 

 

But the column breaks it to look like: 

^p

ABC Welding &
Supply^n
Jane Doe^n

etc. etc. 

 

I want to find a single word (Supply) on it's own line, but has not been forced by a hidden character. Right now I don't know how to find any single word on it's own line. Is there a way to say "New Line" in text not made by a hidden character?

FRIdNGE
July 30, 2021

THIS WORKS EXACTLY LIKE I NEEDED IT! Thank you SO much! 

 

On a side note, do you have a good resource for me to start learning what this coding means? Or where I should go to learn how to write my own scripts?


Other way based on Grep!

 

/*
    _FRIdNGE-0728B_LineRunt.jsx
    by FRIdNGE, Michel Allio [30/07/2021]
*/

var myDoc = app.activeDocument;

var myCondition = myDoc.conditions.item("runt");
if (!myCondition.isValid) myCondition = myDoc.conditions.add ({name: "runt", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT, indicatorColor: [255,186,131]});

app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = ".+";

myFound = myDoc.findGrep();
var F = myFound.length,  f;

for ( f = 0; f < F ; f++ ) if ( myFound[f].lines.length == 2 ) myFound[f].lines[1].appliedConditions = [myCondition];

app.findGrepPreferences = null;

 

(^/)