Skip to main content
dublove
Legend
June 22, 2025
Answered

How to set a color for a character style using a script?

  • June 22, 2025
  • 1 reply
  • 624 views

I want to create new style with some settings like color.

   if (!app.documents[0].characterStyles.item(columnAchaStn).isValid) {
        app.documents[0].characterStyles.add({ name: columnAchaStn });
    }

 

Correct answer rob day

You need to create a new color plate "colorA"  first. 

.I got it right when I set it up manually, but this script doesn't work, I think the code for creating a new color plate doesn't work.

//make a character style then set its properties
var cs = makeCharStyle(app.activeDocument, "columnAchaStn", "colorA")
cs.properties = { fillColor: "colorA", fillTint: 50 }

/**
* If it does't exist, makes a new named CharaterStyle 
* @ param the document to add the style to 
* @ param style name 
* @ return the named character style 
*/

function makeCharStyle(d, n, c) {
    if (d.characterStyles.itemByName(n).isValid) {
        return d.characterStyles.itemByName(n);
    } else {
        return d.characterStyles.add({ name: n });
    }
    var myColorToCheck = app.activeDocument.colors.itemByName(c);
    if (!myColorToCheck.isValid) { app.activeDocument.colors.add({ colorValue: [0, 100, 0, 0], name: c }); };
}

 


There’s also this clever generic collection function suggested by @brian_p_dts  here:

 

https://community.adobe.com/t5/indesign-discussions/how-to-create-various-styles-with-scripts/m-p/15344854#M626385

 

//https://community.adobe.com/t5/indesign-discussions/how-to-create-various-styles-with-scripts/m-p/15344854#M626385

//generic collection maker document, name, collection
var c = makeStyle(app.activeDocument, "colorA", "colors");
c.properties = {space:ColorSpace.CMYK, colorValue:[0,10,0,0]};

var cs = makeStyle(app.activeDocument, "columnAchaStn", "characterStyles")
cs.properties = {fillColor:"colorA"} 



/**
* 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})
    }
}

1 reply

rob day
Community Expert
Community Expert
June 22, 2025

I do something like this:

 

//make a character style then set its properties
var cs = makeCharStyle(app.activeDocument, "columnAchaStn")
cs.properties = {fillColor:"Black", fillTint:50}


/**
* If it does't exist, makes a new named CharaterStyle 
* @ param the document to add the style to 
* @ param style name 
* @ return the named character style 
*/

function makeCharStyle(d, n){
    if (d.characterStyles.itemByName(n).isValid) {
        return d.characterStyles.itemByName(n);
    } else {
        return d.characterStyles.add({name:n});
    }
}

 

dublove
dubloveAuthor
Legend
June 22, 2025

Hi @rob day 

Can't you add it here?
My structure is basically set and I don't want to modify it.
app.documents[0].characterStyles.add({ name: columnAchaStn, fillColor: “red”, });

dublove
dubloveAuthor
Legend
June 22, 2025

That would throw an error of you run it more than once or if a color swatch named 'red' doesn't exist. Also, name needs to be a string—'columnAchaStn' not columnAchaStn:

app.documents[0].characterStyles.add({ name: 'columnAchaStn', fillColor: 'red' });

 


Can it set RGB or CMYK values directly?