Copy link to clipboard
Copied
Hi
I am try to work out if GREP can count X number of line (by the full stops?) then apply a line break. The purpose is to quickly break up walls of text. I have tried searching for an answer to this but suspect I am not phrasing the question correctly to find the answer - can anyone help? it maybe a script rather than GREP that I need
Thanks
Copy link to clipboard
Copied
Script would be probably easiest solution.
I'm not a GREP expert - but you can search for exact number of occurrences of the specified character /phrase - so I think it would be possible?
Copy link to clipboard
Copied
thanks Robert - I was begining to suspect script would be the easiest route. My thinking on GREP was I could put it in a paragraphy style and just apply - less mouse clicks
Copy link to clipboard
Copied
So you have just one loooong paragraph - few pages long?
Copy link to clipboard
Copied
yep - a document full of very long paragraphs some of them 3 to 4 pages
Copy link to clipboard
Copied
And without knowing the contents - you just want to insert paragraph breaks in some random places?
Copy link to clipboard
Copied
yep - a document full of very long paragraphs some of them 3 to 4 pages
Hi @_doctor7 , As others noted a period might not indicate the end of a sentence, but this would add a return after every 4th dotāchange n = 4 to whatever increment you want to use
var n = 4;
var gs = getGrepSearch("\\.")
for (var i = 0; i < gs.length; i++){
if (i % n == n-1) {
ip = gs[i].index+1
gs[i].parent.insertionPoints[ip].contents = "\r"
}
};
/**
* Gets results of a grep search
* @ param grep to search for
* @ return search results as an array
*/
function getGrepSearch(fp){
app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
app.findGrepPreferences.findWhat = fp;
return app.activeDocument.findGrep()
}
Before:
After:
Copy link to clipboard
Copied
thanks Rob - that looks great - its doing what I imagined. Thanks for commenting you GREP, I am already learning a lot from these answers. One question (as a newbie) where are you running that GREP from in indesign?
Copy link to clipboard
Copied
Ctrl+F.
Copy link to clipboard
Copied
thanks
Copy link to clipboard
Copied
My example is a Javascript containing a grep search, which then adds a return after every 4th period foundāyou would have to run the script from the Scripts panel.
Copy link to clipboard
Copied
thanks Rob - that I wouldn't have guessed
Copy link to clipboard
Copied
Oh, you mean a script as a whole - not just a GREP query.
Remember to save it as a plain text in notepad or something.
Copy link to clipboard
Copied
Thanks Robert
Copy link to clipboard
Copied
OK, first off, ciounting full stops does not equal counting lines -- lines wrap according to the column width and if you cahnge that you change the line count.
That said, GREP can count sentences. Expression that seems to work in quick testing is
Find ^((.+?\.){n}) where n= the number of sentences you want to find.
Change to $0\r
This is flawed, though, in that it finds not only the first n sentences of a long paragraph, but also paragraphs of only n sentence length and adds an unwanted extra break at the ends of those. This can be remedied by adding a negative lookahead for a paragrph break:
Find ^((.+?\.){n})(?!\r)
There are still some issues, though. First it fails to delete empty space at what will become the start of the next paragraph.It also doesn't find sentences that end with a question mark or exclamation point, nor does it pick up and keep any quotation marks, footonote references or other things like that which might follow the end-of-sentence punctuation mark.
The first spaces can be eliminated , I think by adding \s* to the xpression:
Find ^((.+?\.){n})(?!\r)\s*
but that sill leaves the problems with sentences that don't end with a full stop. I believe those could be solved using classes, but I haven't tested.
Finally, this expression doesn't care if you break leaving only a single sentence by itself in the next paragraph.
Scripting can probably give you a more nuanced count and vary how many lines to include in each broken paragraph depending on how many it starts with, but that might not be an issue for you.
Copy link to clipboard
Copied
Thanks Peter - I'm a GREP newbie but your explanation was very clear, so although the scripting is probably easier, this is still worth me pursuing
Copy link to clipboard
Copied
Apart from the problems noted by Peter S., dots are used also in abbreviations, initials, ellipses, etc. etc. What you're after looks like a manual job to me.
Copy link to clipboard
Copied
I would concur.
Copy link to clipboard
Copied
Hi @_doctor7, I totally agree with the sentiment expressed by others here about the wisdom of using an automated approach to solve your problemādeciding on what consitutes a paragraph is a job for a writer or editor. Paragraphs have meaning, made of sentences conveying a common idea or theme.
But, for the sake of fun, I will assume that the above doesn't apply in your particular case, and here is a script that adds to Rob's idea but with a few extras. It is still very dumb, so be careful using it.
- Mark
/**
* Splits paragraphs by trying to attempting to find
* paragraphs of active document with many sentences.
* Known Limitations:
* 1. Are you sure this is right for your text? Deciding how many sentences are in a paragraph is the job of a writer or editor, not a script! :)
* 2. Script tries to parse sentences, but is pretty dumb. You must check carefully. Although it leaves numbers alone.
* 3. Script makes no attempt to evaluate the *length* of sentences, just the *number* of sentences.
* by @m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/grep-line-counting/m-p/13850733
*/
var settings = {
targetNumberOfSentencesInParagraph: 3,
}
function main() {
if (app.documents.length == 0)
return;
// A capital letter followed by anything except a period, exclamation or question mark and ending with a word character and then a period, exclamation or question mark.
var findWhat = '((([A-Z][^\\.!?]*\\w[\\.!?]) ){' + settings.targetNumberOfSentencesInParagraph + '})';
app.findGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = findWhat;
// find in every paragraph
// (this breaks up the results by paragraph)
var found = app.activeDocument.stories.everyItem().paragraphs.everyItem().findGrep();
var breaks = [];
paragraphLoop:
for (var i = found.length - 1; i >= 0; i--) {
if (
found[i] == undefined
|| found[i].length < 2
)
// ignore short paragraphs
continue paragraphLoop;
foundLoop:
for (var j = found[i].length - 1; j >= 0; j--)
// collect these insertion points
breaks.push(found[i][j].insertionPoints.lastItem());
}
var counter = 0;
for (var i = 0; i < breaks.length; i++) {
// add the carriage return
breaks[i].contents = '\r';
counter++;
}
alert(counter + ' carriage returns added.');
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Split Paragraphs');
Copy link to clipboard
Copied
Thanks Mark
I do totally agree with yours and everyoneās comments, but no this isn't written to be read in any normal way, its lines of text from a data crunching program, (sorry I'm not allowed to post an example piece) I just want it to look nicer on the page with a vague hope that it relieves the eye strain on people who do scan through it, where the breaks are is completely irrelevant and yes, I could do it manually randomly but itās a time thing.
Copy link to clipboard
Copied
In this case - is there a specific text that could be treated as a marker for the new line? Either before or after it?
Copy link to clipboard
Copied
sorry Robert, but no. Common to most sentances is the word' data' but its not always in the same place
Copy link to clipboard
Copied
Doesn't have to be - I'm just suggesting use of something that will split your wall-of-data in more useful / "logic" way - not just Nth dot.
Isn't there something that starts a row / record of the data?
There needs to be some kind of structure for your data?
Copy link to clipboard
Copied
Well, if it's like that you don't need a script at all. This GREP query breaks a text after every fourth dot:
Find what: (.+?\.){4}\K\x20
Change to: \r
(\x20 denotes the space character.)
The number 4 determines the number of dots after which to place the paragraph break, so if you want more or fewer paragraph breaks, change that number.
This one breaks after dots only. To break after exclamation points and question marks as well, replace \. with [.?!].
P.
Copy link to clipboard
Copied
Thanks Peter - I think that this is the solution to go for. Don't know if its possible to reply to every one in this thread, but I would like to thank every one who took the time to reply and the education is has provided.