Skip to main content
dublove
Legend
May 28, 2025
Answered

How to create various styles with scripts?

  • May 28, 2025
  • 3 replies
  • 1027 views

I want to determine if the styles below exist before I run another script, and if they don't, then create new ones.

Paragraph style: pa
Character style: ch
Object style: ob
Table style:: mytable
cell style: cel

 

Thank you very much.

Correct answer brian_p_dts

Can you tell me the table style, cell object style their names.

 

You can search the entire scripting API. Here are the ExtendScript document methods and objects:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html

 

To find proberties there is the document’s cellStyle, tableStyle, paragraphStyle, characterStyle, ObjectStyle.

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#CellStyle.html#d1e456077

 

 Note for creating a new style you use the plural (i.e., d.cellStyles.add())

 

Here is a makeCellStyle function:

 

var cs = makeCellStyle(app.activeDocument, "My Cell Style");
//set the properties of the style named cs
cs.properties = {fillColor:"Black"};


/**
* Makes a new named Cell Style or get the style named n
* @ param the document to add the style to 
* @ param style name 
* @ returns the new cell style 
*/

function makeCellStyle(d, n){
  var cs;
  try {
      d.cellStyles.add({name:n});
  }catch(e) {
      cs = d.cellStyles.itemByName(n);
  } 
  return d.cellStyles.itemByName(n);
}

 

 

 

 

 

You can also have a universal function by referencing the document's relevant property via a string reference: 

 

var makeStyle = function(doc, styleName, collection) {

    doc[collection].add....

}

 

makeStyle(app.activeDocument, "pa", "paragraphStyles");

3 replies

rob day
Community Expert
Community Expert
May 28, 2025

Paragraph style: pa

 

Hi @dublove , I use this function to get or set a style. This makes a new style named "pa", then I can set the new or existing style’s properties. You can do the same for Character and Object styles

 

var ps = makeParaStyle(app.activeDocument, "pa");
//set the properties of the style named pa
ps.properties = {basedOn:"[No Paragraph Style]", appliedFont:"Myriad Pro	Regular", pointSize:8, leftIndent:0};



/**
* Makes a new named Paragraph Style or get the style named n
* @ param the document to add the style to 
* @ param style name 
* @ returns the new paragraph style 
*/

function makeParaStyle(d, n){
  var ps;
  try {
      d.paragraphStyles.add({name:n});
  }catch(e) {
      ps = d.paragraphStyles.itemByName(n);
  } 
  return d.paragraphStyles.itemByName(n);
}
dublove
dubloveAuthor
Legend
May 28, 2025

This one's good.
Thank you very much.
Can you tell me the table style, cell object style their names.
I went through so many scripts written by others to get to the last sentence.

rob day
Community Expert
Community Expert
May 28, 2025

You can also have a universal function by referencing the document's relevant property via a string reference: 

 

var makeStyle = function(doc, styleName, collection) {

    doc[collection].add....

}

 

makeStyle(app.activeDocument, "pa", "paragraphStyles");


Hi @brian_p_dts , Thanks that works great. I think @dublove , still would need to check if the style already exists (I get an error running more than once) so maybe this?

 


makeStyle(app.activeDocument, "pa", "paragraphStyles");


/**
* Make a new collection 
* @ param the document 
* @ param the name 
* @ param the collection class 
* @ return the named collection 
*/
function makeStyle(d, n, collection) {
    if (d[collection].itemByName(n).isValid) {
        return d[collection].itemByName(n);
    } else {
        return d[collection].add({name:n})
    }
}
Willi Adelberger
Community Expert
Community Expert
May 28, 2025

What advantage do you expect with scripting styles? What should the script do what you can't do without scripting?

brian_p_dts
Community Expert
Community Expert
May 28, 2025

Depends if styles are in groups, but if not: 

if (app.activeDocument.paragraphStyles.itemByName("pa").isValid) { ...

dublove
dubloveAuthor
Legend
May 28, 2025

Are other style judgments similar?

brian_p_dts
Community Expert
Community Expert
May 28, 2025

Try it and report back!