how to find last word(<=8 characters) of each paragraph?

Community Beginner ,
Aug 19, 2022 Aug 19, 2022

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).

TOPICS
Scripting

Views

331

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 22, 2022 Aug 22, 2022

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.

Likes

Translate

Translate
Community Beginner ,
Aug 19, 2022 Aug 19, 2022

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).

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 20, 2022 Aug 20, 2022

Copy link to clipboard

Copied

Look for

 

\s\K\S{1,8}$

 

and apply no-break (set it in the Change format panel).

P.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 20, 2022 Aug 20, 2022

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);

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 20, 2022 Aug 20, 2022

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. 

---
Can automate anything - as long as it doesn't require A.I. 😉 and even then 3rd helping hand can be created 😉

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 21, 2022 Aug 21, 2022

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}$";

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

@Peter Kahrel Wouldn't this be better as a GREP style in a paragraph style?

 

David Creamer: Community Expert, Adobe Certified Instructor, and Adobe Certified Expert (since 1995)

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

@Creamer Training -- 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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

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. 

 

 

David Creamer: Community Expert, Adobe Certified Instructor, and Adobe Certified Expert (since 1995)

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 21, 2022 Aug 21, 2022

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.

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 21, 2022 Aug 21, 2022

Copy link to clipboard

Copied

You can then make it smarter and have digits never be on a end of line by themselves

EugeneTyson_0-1661069282678.png

 

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

But, Eugene, they're looking for last single words that are fewer than 8 characters.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

I was going by the example in the 2nd post. Which is different to last 8 characters.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 22, 2022 Aug 22, 2022

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.

 

Arunkumar25715058ufpl_0-1661178461075.png

 

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?

Arunkumar25715058ufpl_1-1661178827009.png

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 22, 2022 Aug 22, 2022

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. 

---
Can automate anything - as long as it doesn't require A.I. 😉 and even then 3rd helping hand can be created 😉

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

I believe it substitute a \s for an actual space.

You can see the gap in the code.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

Hi Eugene,

Yes, i tried to replace empty space instead of \s both are not working.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 22, 2022 Aug 22, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

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...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

Trailing white space -- ugh. In that case, as indeed in your example, add \s* before the $

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines