Search code examples
javascriptadobeextendscriptadobe-indesign

How can I programmatically set text to "Small Caps" in InDesign with ExtendScript?


I'm trying to do the equivalent of selecting some text and clicking on the Small Caps button. Here are a few attempts:

app.activeDocument.textFrames[0].texts.appliedCharacterStyle.capitalization = Capitalization.SMALL_CAPS;

Doesn't work: "Invalid request on a root style"

var myCharacterStyle = new CharacterStyle();
myCharacterStyle.capitalization = Capitalization.SMALL_CAPS;
app.activeDocument.textFrames[0].texts[0].applyCharacterStyle(myCharacterStyle);

Doesn't work: "Invalid value for parameter 'using' of method 'applyCharacterStyle'. Expected CharacterStyle, but received nothing."

How am I supposed to do this?


Solution

  • You need to start by creating a new character style if you want to change the default "[None]"

    //First create a new character style
    var newCharacterStyle = document.characterStyles.add(text.appliedCharacterStyle);
    if(text.appliedCharacterStyle!=null){
        newCharacterStyle.basedOn = text.appliedCharacterStyle;
    }   
    text.appliedCharacterStyle = newCharacterStyle;
    
    //then apply capitalization
    newCharacterStyle.capitalization = Capitalization.SMALL_CAPS;