Skip to main content
Inspiring
September 4, 2015
Answered

add tab character before particular paragraph style content

  • September 4, 2015
  • 3 replies
  • 734 views

I need to add tab character before particular paragraph style content

app.findTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("IDX2"); 

list = app.activeDocument.findText(); 

// 2. For each found item ... 

for (i=0; i<list.length; i++) 

app.select(list)

app.selection[0].insertionPoints[0].contents="\t"

 

}

But the above code only working in the first occurance. But I need to add the tab character all the applied paragaph content

How to do this

This topic has been closed for replies.
Correct answer Ronald63

Hi,

Another way without GREP ...

var doc = app.activeDocument;

var myStories=doc.stories;

var J= myStories.length;

while( J-- ){

    var mStorie = myStories;

    var K= mStorie.paragraphs.length

    while( K-- ){

        if  (mStorie.paragraphs.appliedParagraphStyle.name == "toto") {

            mStorie.paragraphs.contents= "\t" + mStorie.paragraphs.contents;

        }

     } 

}

3 replies

Brainiac
September 5, 2015

The answer that you flagged as correct has one flaw: it messes up any formatting in a paragraph because the script replaces the contents of whole paragraphs.

Pawel's script doesn't have that problem. I would prefer Pawel's script anyway -- why would you use two while loops just to avoid a grep expression?

Peter

Brainiac
September 5, 2015

Hi Peter,

I would say a huge flaw ...

Thank you for your vigilance


Ronald

Inspiring
September 7, 2015

Thank your great support peter.

Ronald63Correct answer
Brainiac
September 4, 2015

Hi,

Another way without GREP ...

var doc = app.activeDocument;

var myStories=doc.stories;

var J= myStories.length;

while( J-- ){

    var mStorie = myStories;

    var K= mStorie.paragraphs.length

    while( K-- ){

        if  (mStorie.paragraphs.appliedParagraphStyle.name == "toto") {

            mStorie.paragraphs.contents= "\t" + mStorie.paragraphs.contents;

        }

     } 

}

Participating Frequently
September 4, 2015

Hi,

Please try this snippet:

var doc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.properties = {findWhat: '^(.)', appliedParagraphStyle: "IDX2"};

app.changeGrepPreferences.changeTo = '\\t$1';

doc.changeGrep();

regards

Pawel Zelezniakowicz