Skip to main content
Inspiring
September 4, 2015
Answered

add tab character before particular paragraph style content

  • September 4, 2015
  • 3 replies
  • 750 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

Peter Kahrel
Community Expert
Community Expert
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

Community Expert
September 5, 2015

Yes, Peter.

I'd never replace the whole contents of a paragraph assigning contents with the = operator that does not care at all about existing formatting.

That's outright "dangerous". If someone would like to use Ronald's solution it could be done in a "minimal invasive" way:
Just assign the tab as the contents of the first insertion point of the found paragraph.

Uwe

Ronald63Correct answer
Legend
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