Copy link to clipboard
Copied
I need to find last word of each paragraph. Also check lastword is <= 8 characters only( include special character, dot and numbers). Find and apply no break condition.
for example.
Common (may affect up to 1 in 10 pat):
- Severe kidney failure (this will be ascertained by your doctor through some specific blood test)
- Low levels of calcium in the blood.
Uncommon (may affect up to 1 in 100 patients):
how many mg of each tablet is in a bottle?
Each tablet contains 100 mg.
If you enter number of paragraphs or lines higher than 200 –the function will work but will produce only 200 paragraphs (10).
If you don't want to include closing punctuation in the character count of the last word I would add [[:punct:]]* which will find any punctuation if it exists (* is the zero or more times match), and maybe add \s* as well in case the original text has extraneous trailing whitespace, so the final code would look like
\s\K\S{1,8}[[:punct:]]*\s*$
This would still fail if there is whitespace before any ending punctuation.
Copy link to clipboard
Copied
I need to find and apply no breaks condition for below mentioned words.
10 pat):
blood test)
the blood.
a bottle?
100 mg.
paragraphs (10).
Copy link to clipboard
Copied
Look for
\s\K\S{1,8}$
and apply no-break (set it in the Change format panel).
P.
Copy link to clipboard
Copied
Hi Peter,
I tried your suggestion, but it is not working. I attached my xml, indesign file and javascript code for your reference. Also, I tried manually but it is also not working.
var doc = app.activeDocument;
function testing(doc){
//Clear the find/change text preferences.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
//Set the GREP find options (adjust to taste)
app.findChangeGrepOptions.includeFootnotes = true;
app.findChangeGrepOptions.includeHiddenLayers = true;
app.findChangeGrepOptions.includeLockedLayersForFind = true;
app.findChangeGrepOptions.includeLockedStoriesForFind = true;
app.findChangeGrepOptions.includeMasterPages = true;
//Look for the pattern and change to
app.findGrepPreferences.findWhat = "\s\K\S{1,8}$";
var myFoundItems = app.activeDocument.findGrep();
for(var n=0;n<myFoundItems.length;n++)
{
var getContent = myFoundItems[n].contents;
alert(getContent);
app.findGrepPreferences.findWhat = getContent;
app.changeGrepPreferences.noBreak = true;
app.activeDocument.changeGrep();
app.changeGrepPreferences = NothingEnum.nothing;
}
//Clear the find/change text preferences.
app.findGrepPreferences = NothingEnum.nothing;
//app.changeGrepPreferences = NothingEnum.nothing;
}
testing(doc);
Copy link to clipboard
Copied
You don't need script for that - @Peter Kahrel's reply was for use in Find&Change dialog.
I'm pretty sure, you need to write it a bit it differently when used in script.
Copy link to clipboard
Copied
You have to double-escape backslashes when passing as a string in jsx:
app.findGrepPreferences.findWhat = "\\s\\K\\S{1,8}$";
Copy link to clipboard
Copied
@Peter Kahrel Wouldn't this be better as a GREP style in a paragraph style?
Copy link to clipboard
Copied
@Dave Creamer of IDEAS -- Probably. But the question was about the GREP expression to use, and the same expression can be used in a script and in a GREP style. When you set finalised text I'd favour a script because GREP expressions always come with performance penalties. Sometimes those penalties are worth it, sometimes not.
Copy link to clipboard
Copied
@Peter Kahrel I tend to favor a style since the no break could result in slight text reflow. Running as a script, I see two potential issues: remembering to run it before proof versions, and dealing with reflow later in the production cycle. One would have to balance performance issues against those issues. Of course, workflow and system hardware could affect one's decision too.
Copy link to clipboard
Copied
This will find the last two words of a paragraph
\<(\s?(\S+)){2}$
You can then apply the no break.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
But, Eugene, they're looking for last single words that are fewer than 8 characters.
Copy link to clipboard
Copied
I was going by the example in the 2nd post. Which is different to last 8 characters.
Copy link to clipboard
Copied
Hi Eugene,
I tried your suggestion. but it is not working. Please refer the below screenshot. I am using Adobe InDesign 2020.
Finally I find this \s\H+[.\w\d\l\u]+\W*?$ - it is working but it is finds last word in all para. how to add 8 character only in this script?
Copy link to clipboard
Copied
You need to check how many text lines is in the paragraph - if more than 1 then you should apply NoBreak.
Copy link to clipboard
Copied
Peter Kahrel's code above findes the last word of a paragraph only if it contains 1 to 8 characters, but it will include closing punctuation in the count.
Copy link to clipboard
Copied
If you don't want to include closing punctuation in the character count of the last word I would add [[:punct:]]* which will find any punctuation if it exists (* is the zero or more times match), and maybe add \s* as well in case the original text has extraneous trailing whitespace, so the final code would look like
\s\K\S{1,8}[[:punct:]]*\s*$
This would still fail if there is whitespace before any ending punctuation.
Copy link to clipboard
Copied
I believe it substitute a \s for an actual space.
You can see the gap in the code.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi Peter Spier,
I tried your script( \s\K\S{1,8}\s*$ ). It is working fine. Suppose, I search last word with empty space( for example: \spat): ). when I added \s before the script it is not working.
Copy link to clipboard
Copied
Not quite sure where or why you are adding another \s before the expression. The unmodified expression starts by finding a space, so adding another seems unnecessary and likely to cause failure because you would now need two spaces before the last word.
Copy link to clipboard
Copied
\< is the beginning of a word, so I think that while the ? tells the epression the character might not exist, the \s might be causing a problem in that code -- words never start with a space.
Peter K can probably tell better...
Copy link to clipboard
Copied
Sorry, Eugene, you're right: what Arunkumar is after is applying no break to paragraph-final words of 8 characters or fewer and the preceding word. If that's the case, this would do:
.\s\S{1,8}$ // In a GREP style
.\\s\\S{1,8}$ // In a script
In other words, any character followed by a space followed by 8 non-space characters, at paragraph-end.
Copy link to clipboard
Copied
Peter, I think that this should be modified with the additions I suggested above, particularly for the possibility of trailing white space unless Arunkumar25715058ufpl has already run a cleanup script to correct for that.
Copy link to clipboard
Copied
Trailing white space -- ugh. In that case, as indeed in your example, add \s* before the $