Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to create various styles with scripts?

Guide ,
May 27, 2025 May 27, 2025

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.

TOPICS
Bug , Feature request , How to , Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 3 Correct answers

Community Expert , May 28, 2025 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
* @ 
...
Translate
Community Expert , May 28, 2025 May 28, 2025

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.cell

...
Translate
Community Expert , May 28, 2025 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");

Translate
Community Expert ,
May 27, 2025 May 27, 2025

Depends if styles are in groups, but if not: 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 27, 2025 May 27, 2025

Are other style judgments similar?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2025 May 28, 2025

Try it and report back!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 28, 2025 May 28, 2025
The name didn't change to pa.
if(app.documents[0].paragraphStyles.item("pa").isValid)
{var a=6;}
else{
app.activeDocument.paragraphStyles.add("pa");
}
 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 28, 2025 May 28, 2025

 

if(app.activeDocument.paragraphStyles.itemByName("pa").isValid)
{var a=6;}
else{
app.activeDocument.paragraphStyles.add({name:"pa"});
}

There's a parenthesis missing. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2025 May 28, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2025 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);
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 28, 2025 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2025 May 28, 2025

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);
}

 

 

 

 

 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2025 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");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2025 May 28, 2025

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})
    }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 28, 2025 May 28, 2025

Hi  brian_p_dts      rob day 

Thank you very much.

That's enough.
I actually had enough with just this form:

if(app.activeDocument.paragraphStyles.itemByName("pa").isValid)
{var a=6;}
else{
app.activeDocument.paragraphStyles.add({name:"pa"});
}


I'm actually still stuck at the level of thinking of one command in one sentence.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2025 May 28, 2025

I'm actually still stuck at the level of thinking of one command in one sentence.

 

Then you shouldn’t need the if else, you can do if not (! before the statement):

 

if(!app.activeDocument.paragraphStyles.itemByName("pa").isValid) {
    app.activeDocument.paragraphStyles.add({name:"pa"})
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 28, 2025 May 28, 2025
LATEST

Another trick I learned.
Thank you,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines