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
After
Already thanks for you'r help.
Greetings.
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
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
Copy link to clipboard
Copied
Hi Manan,
That worked perfectly!
Many thanks.
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.
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
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
Becomes this
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.
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
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.
Copy link to clipboard
Copied
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