Skip to main content
Participant
April 19, 2016
Question

Write Para and character styles in txt file

  • April 19, 2016
  • 4 replies
  • 558 views

Hi All,

I am very beginner in inDesign scripting and i just practicing the basic scripts.

i use the below code for collecting all the paragraph and character styling.

is this correct and secured way to collect all styles? can any one suggest me.

app.activeDocument.paragraphStyles.name;

app.activeDocument.characterStyles.name;

----

My Full script

var doc=app.activeDocument;

var docPath = doc.filePath.fsName;

var paraCount=doc.paragraphStyles.length;

var charcount=doc.characterStyles.length;

writeFile ();

function writeFile(){

var fh=File(docPath+"\\StyleList.txt");

fh.open("w");

if(fh!=-1){

//Paragraph style list Append

    var strgP="";

    for(var i=0; i < paraCount; i++){

        var k=i+1;

        var styleName = doc.paragraphStyles.name;

        strgP += "Para style No " +k +": "+styleName + "\n";

    }

//character style list append

    var strgC="";

    for(var i=0; i < charcount; i++){

        var k=i+1;

        var styleName = doc.characterStyles.name;

        strgC += "Char style No " +k +": "+styleName + "\n";

    }

    fh.write(strgP +"\n\n"+ strgC);

    fh.close();

    }

else{

    alert("Style file not created ...");

   }

}

Thanks in Advance,

Arasu

This topic has been closed for replies.

4 replies

Vamitul
Legend
April 19, 2016

Hi Arasu,

One approach is to use the doc.paragraphStyles

doc.paragraphStyles will return a collection of paragraph styles, so you can do fun things like:

var listOfNames=doc.paragraphStyles.everyItem().name.join('\n')

It does have one major disadvantage however: it will only return the root paragraph styles, and not the ones inside style groups.

The other approach is to use the doc.allParagraphStyles. This  will return all the paragraph styles in the document, regardless of their nesting, but has the disadvantage of being a plain array, so to do anything with it you will need to iterate through it (and since ExtendScript is based on JavaScript 1.5 you can't even use array.forEach).

The best approach depends on what exactly you need.

Participant
April 20, 2016

Thank you so much Vamitul .

Thanks,

Arasu

Participant
April 19, 2016

Thank you so much tpk and Karthick...

Arasu

tpk1982
Legend
April 19, 2016

Hi Arasu,

Yes you are in the right track..

karthikS
Inspiring
April 19, 2016

HI

Try this

var doc = app.activeDocument,

    Pstyles = doc.allParagraphStyles,

    report = "";

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

{

        report += Pstyles.name + "\r ";

    }

var file = new File(doc.fullName.toString().replace(".indd",".txt"));

file.open('w');

file.write(report);

file.close()

alert("Process Done");

/////////////////////////////