Skip to main content
Inspiring
September 23, 2021
Answered

Format paragraph depending on the first word

  • September 23, 2021
  • 5 replies
  • 1502 views

Hi.

 

I want to select paragraphs to be formatted (or one word has to be deleted in) depending on the first word in that paragraph (if it is a 6-digit number).

Does it work with regex or better with findGrep?

This topic has been closed for replies.
Correct answer Peter Kahrel

Tru this:

app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '^\\d{6}\\b.+?\\K\\b(Blue|Yellow|Red)\\b';
colours = app.documents[0].findGrep();
for (i = colours.length-1; i >= 0; i--) {
  colours[i].contents = colours[i].contents.toLowerCase();
}

 

Where it says (Blue|Yellow|Red) insert your own colour names. Grep searches are case-sensitive, so you find only the words with an initial capital, so there's no need to chack anything. Simply lower-case whatever you find.

 

P.

5 replies

cmoke73Author
Inspiring
September 30, 2021

I don't wanna push anybody... 😉

Peter KahrelCorrect answer
Adobe Expert
September 30, 2021

Tru this:

app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '^\\d{6}\\b.+?\\K\\b(Blue|Yellow|Red)\\b';
colours = app.documents[0].findGrep();
for (i = colours.length-1; i >= 0; i--) {
  colours[i].contents = colours[i].contents.toLowerCase();
}

 

Where it says (Blue|Yellow|Red) insert your own colour names. Grep searches are case-sensitive, so you find only the words with an initial capital, so there's no need to chack anything. Simply lower-case whatever you find.

 

P.

cmoke73Author
Inspiring
October 8, 2021

I'm sorry for being late. But the flu made me inoperative. 🙂

 

That was a good hint Peter. Thanks.

But take a look at the result...

 

before:

 

 

 

 

 

 

after:

 

 

 

 

 

 

 

The second word was not changed. If I fill it up with more lines, only in every second paragraph (line) the word will be change.

 

 

#UPDATE:

It was the ^ (at the begining of the paragraph)

I don´t know why the algorithm did´n like that. I would be more relaxed if the expression would be as exact as possible. 🙂

Brainiac
September 24, 2021

You said "line".

Line or paragraph?

 

That would make a significant distinction.

If paragraph your script could be done very efficient.

If line it will turn to a slow one if it must run on a lot of text.

 

In German:

Also überlege Dir einen Algorithmus, der in einzelnen kleinen Schritten aufgedröselt genau das macht was Du möchtest. Von A bis Z. Ohne dass Dich zunächst Fragen nach seiner Umsetzung quälen. Also kümmere Dich erst mal nicht darum wie Du einen Absatz ansteuerst. Denk' nur darüber nach, dass Du es tun must (oder meinst es tun zu müssen).

 

Poste diesen Algorithmus, jeder Schritt ein Absatz, dann können wir darüber nachdenken wie das in Code umzusetzen ist. Oder ob ein neuer Lösungsansatz zu formulieren ist.

 

Regards,
Uwe Laubender

( ACP )

cmoke73Author
Inspiring
September 24, 2021

I meant "paragraph"

sorry!

 

 

"...Poste diesen Algorithmus..." --> siehe oben

Brainiac
September 24, 2021

"That´s what I want to do: a list of the colors whose names should be changed. Than do it with the script."

 

From your description I'm not sure what should follow.

Can you write down the algorithm in plain words?

For the whole task. From A to Z.

 

Thanks,
Uwe Laubender

( ACP )

cmoke73Author
Inspiring
September 24, 2021

 

- generate an array (I can expand if I need) of color names

- search in a loop for paragraphs (only those beginning with 6 numbers) which contains a word (color name) after: (^\d{6}.\t) and before (~y€\s\d{1,},\d{2}$) --> every line contains only one color at that place (sometimes connected with a hyphen, unfortunately)

- if the word is at the list (array) and first character is upper case --> change it to lower case


I hope the explanation is understandable 🙂

Brainiac
September 23, 2021

Hi cmoke73,

so, if neededWord is always the same and if you can live with the result in the left and selected frame:

 

 

you can solve it with a GREP style with pattern:

\d{6,}\s.+\KneededWord

 

Unfortunatelly this pattern is not working:

^\d{6,}\s.+\KneededWord

 

But you could try to counter the effect of the first GREP style with another one, see the right frame of the two:

 

Pattern of the second GREP style:

.\d{6,}\s.+\KneededWord

 

\K acts as a flexible positive look behind.

Available since InDesign CS6 v8.0.

 

Regards,
Uwe Laubender

( ACP )

cmoke73Author
Inspiring
September 24, 2021

I think that wouldn't work Uwe. Can't imagine how to change with a GREP within a paragraph style only the first character of certain words from upper case to lower case.

There are a lot of colors I have to change occasionally but also other words (like silver, in german Silber) with capitalized first letter. So I have to do that with a script including a list of colors have to be changed.

My idea was to do the changes depending on the first 6 characters in each line: every line with 6 digits at beginning is a candidate. And there is my problem, I don´t know how to select.

jmlevy
Adobe Expert
September 23, 2021

Can you show an exemple?

cmoke73Author
Inspiring
September 23, 2021

Headline

Text Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text Text 

123456 Text Text neededWord Text 

987456 Text neededWord Text Text 

589314 Text Text neededWord Text 

 

Want to get/change just the red words (exemple) in the lines which begins with six numbers.

There are names for colors with a capital letter at the beginning which I would like to change to lower case letters.

 

jmlevy
Adobe Expert
September 23, 2021

It would be easy to find any paragraph beginning with 6 digits but it will not allow you to find a specific word into them, especially if these words don't immediately follow the digits.

If you know what color name has to be changed, it's easier to run a “classical” find/change : find Blue, replace it by blue and so on.

Maybe someone will have a brilliant idea…