Shawn
Shawn

Reputation: 11391

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?

Upvotes: 1

Views: 1967

Answers (2)

martinrohart
martinrohart

Reputation: 161

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;

Upvotes: 2

Shawn
Shawn

Reputation: 11391

I found a buggy way to set Small Caps on text:

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

I say buggy because instead of setting Small Caps only on the wanted text, the whole paragraph is styled. This is the case because I use appliedParagraphStyle instead of appliedCharacterStyle. But when I use appliedCharacterStyle, I get "Invalid request on a root style." because the current CharacterStyle is that special one called "[None]". The "[None]" CharacterStyle doesn't allow writing to its appliedCharacterStyle property, nor does it allow duplication (with duplicate()).

Upvotes: 0

Related Questions