• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Format paragraph depending on the first word

Engaged ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

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?

TOPICS
How to , Performance , Scripting

Views

717

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 , Sep 30, 2021 Sep 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.

Votes

Translate

Translate
Community Expert ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

Can you show an exemple?

Votes

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
Engaged ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

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.

 

Votes

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 ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

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…

Votes

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
Engaged ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

quote

...

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.

...


By @jmlevy

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

Votes

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 ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

Hi cmoke73,

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

 

neededWord-GREP-Style-Pattern-1.PNG

 

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:

 

neededWord-GREP-Style-Pattern-2.PNG

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 )

Votes

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
Engaged ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

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.

Votes

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 ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

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

Votes

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
Engaged ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

 

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

Votes

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 ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

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 )

Votes

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
Engaged ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

I meant "paragraph"

sorry!

 

 

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

Votes

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
Engaged ,
Sep 30, 2021 Sep 30, 2021

Copy link to clipboard

Copied

I don't wanna push anybody... 😉

Votes

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 ,
Sep 30, 2021 Sep 30, 2021

Copy link to clipboard

Copied

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.

Votes

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
Engaged ,
Oct 08, 2021 Oct 08, 2021

Copy link to clipboard

Copied

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:

Bildschirmfoto 2021-10-08 um 14.30.39.png

 

 

 

 

 

 

after:

Bildschirmfoto 2021-10-08 um 14.30.47.png

 

 

 

 

 

 

 

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

Votes

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
Engaged ,
Oct 11, 2021 Oct 11, 2021

Copy link to clipboard

Copied

... and my next question would be if it´s possible to create an array (expandeble) with the colors and insert it in the loop. something like:

 

   var myDoc = app.activeDocument;
    var mySelection = myDoc.selection[0];
    
    var farben = [['Rot'],['Blau'],['Grün'],['Schwarz'],['Weiß'],['Gelb'],['Orange'],['Braun']];

 scriptTimer();
 
 function scriptTimer(){
    app.findGrepPreferences = null;
    app.findGrepPreferences.findWhat = '\\d{6}.+?\\t\\K\\b('+farben+')\\b' ; 
        colours = mySelection.findGrep();
        for (i = colours.length-1; i >= 0; i--) {
          colours[i].contents = colours[i].contents.toLowerCase();
        }
    }

        app.findGrepPreferences = NothingEnum.nothing;     
        app.changeGrepPreferences = NothingEnum.nothing; 

 

... of course, this didn´t work.

Votes

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 ,
Oct 11, 2021 Oct 11, 2021

Copy link to clipboard

Copied

Sure:

 

var farben = ['Rot','Blau','Grün','Schwarz','Weiß','Gelb','Orange','Braun'];
. . .
. . .
app.findGrepPreferences.findWhat = '\\d{6}.+?\\t\\K\\b(' + farben.join('|') + ')\\b';

Votes

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
Engaged ,
Oct 11, 2021 Oct 11, 2021

Copy link to clipboard

Copied

LATEST

So cool! It's going great now.

Thank´s Peter.

Votes

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