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

Add text if ParagraphStyle is found

Participant ,
Oct 22, 2018 Oct 22, 2018

Copy link to clipboard

Copied

Dear scripters,

Can you let Indesign to look for an specific paragraph style and if found than add an specific text before that line see picture below.

Before

before.png

After

after.png

Already thanks for you'r help.

Greetings.

TOPICS
Scripting

Views

926

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 , Oct 22, 2018 Oct 22, 2018

Try the following code

function addTextToPara(pStyleName, prefixText)

{

       app.findGrepPreferences.appliedParagraphStyle = pStyleName

       var paraList = app.activeDocument.findGrep()

       for(var i = 0 ; i < paraList.length; i++)

       {

            paraList.contents = prefixText + paraList.contents

       }

       app.findGrepPreferences = NothingEnum.nothing;

}

//Change the argument with the name of the paragraph style you want to search and the text you want to add before that para

addTextToPar

...

Votes

Translate

Translate
Community Expert ,
Oct 22, 2018 Oct 22, 2018

Copy link to clipboard

Copied

Try the following code

function addTextToPara(pStyleName, prefixText)

{

       app.findGrepPreferences.appliedParagraphStyle = pStyleName

       var paraList = app.activeDocument.findGrep()

       for(var i = 0 ; i < paraList.length; i++)

       {

            paraList.contents = prefixText + paraList.contents

       }

       app.findGrepPreferences = NothingEnum.nothing;

}

//Change the argument with the name of the paragraph style you want to search and the text you want to add before that para

addTextToPara("Paragraph Style 2", "<1>")

-Manan

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
Participant ,
Oct 22, 2018 Oct 22, 2018

Copy link to clipboard

Copied

Hi Manan,

That worked perfectly!

Many thanks.

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
Participant ,
Oct 28, 2018 Oct 28, 2018

Copy link to clipboard

Copied

Hi Manan and others,

Is there also an way the text would be at the end of an paragraph?

Like <1>here is some text<1>

Again thanks for you'r help!

Greetings.

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 28, 2018 Oct 28, 2018

Copy link to clipboard

Copied

In place of the below line in the code mentioned before in the thread use the line that follows it

//Replace this

paraList.contents = prefixText + paraList.contents

//With this

paraList.contents = prefixText + paraList.contents + prefixText

-Manan

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
Participant ,
Oct 28, 2018 Oct 28, 2018

Copy link to clipboard

Copied

Wouw that's an extremely fast answer! Thanks!

It is placing the text alright but at the beginning of an new paragraph.

This

Schermafbeelding 2018-10-28 om 17.12.28.png

Becomes this

Schermafbeelding 2018-10-28 om 17.12.11.png

I'm wondering if there is an way you can tell indesign that there is an end of an paragraph (without using /n or /r)?

And if found add <2> so you get <2>TEST<2>.

Greetings.

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 28, 2018 Oct 28, 2018

Copy link to clipboard

Copied

I am not sure why you don't wanna use \r or \n for check condition. The following works for me

function addTextToPara(pStyleName, prefixText) 

       app.findGrepPreferences.appliedParagraphStyle = pStyleName 

       var paraList = app.activeDocument.findGrep(true) 

       for(var i = 0 ; i < paraList.length; i++) 

       {

               if(paraList.contents[paraList.contents.length - 1] == '\r')

                    paraList.insertionPoints[-2].contents =  prefixText

               else

                    paraList.insertionPoints[-1].contents = prefixText

               paraList.contents = prefixText + paraList.contents

       } 

       app.findGrepPreferences = NothingEnum.nothing; 

 

//Change the argument with the name of the paragraph style you want to search and the text you want to add before that para 

addTextToPara("Paragraph Style 2", "<1>")

-Manan

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
Participant ,
Oct 28, 2018 Oct 28, 2018

Copy link to clipboard

Copied

Oh, but this works perfectly, can I double give you the credits?

The reason I'm scared to use \r is that sometimes in the story there is an \r and sometimes an \n.

I thought when you use that variable the script puts in the middle of story 's an <1> and I only wanted it at the end of each lines of text with an specific paragraph style but that's not the case, I see now.

You look for an specific paragraph style and the end of each paragraph style it always ends with an \r. I was thinking wrong.

Again for the second time thanks for you'r help!

Greetings.

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 29, 2018 Oct 29, 2018

Copy link to clipboard

Copied

LATEST

Hi beh_gras_beh ,

is this thread related to the one below? Guess, yes:

GREP style in paragraph with ~b \r or \n?

Regards,
Uwe

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