Skip to main content
Known Participant
March 13, 2025
Answered

Number of character styles applied to a word - help required please

  • March 13, 2025
  • 1 reply
  • 1378 views

Hi eveyone,

Using the code below I can see the name of the character style is applied to a word.

var mySelection = app.selection[0];
alert(mySelection.words[6].appliedCharacterStyle.name)

The code above seems to pick up the character style applied to the first part of it.

 

What I'm trying to do is check if there is more that 1 character style applied to a single word but, as yet I've not managed.

For example, 'posted', has 2 character styles applied - plain and bold. I'm trying to get that count in InDesign via a script.

Is it possible?

Thanks in advance.

 

Correct answer m1b

Hi @Nick37129562ryen, the object you are looking for is TextStyleRange. You can use it in your example like this:

var mySelection = app.selection[0];
var myWord = mySelection.words[6];
var textStyleRanges = myWord.textStyleRanges;

for (var i = 0; i < textStyleRanges.length; i++) {
    var tsr = textStyleRanges[i];
    alert(tsr.appliedCharacterStyle.name);
}

Does that make sense? TextStyleRanges are ranges of text that have exactly the same character attributes and styles applied.

- Mark

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
March 13, 2025

Hi @Nick37129562ryen, the object you are looking for is TextStyleRange. You can use it in your example like this:

var mySelection = app.selection[0];
var myWord = mySelection.words[6];
var textStyleRanges = myWord.textStyleRanges;

for (var i = 0; i < textStyleRanges.length; i++) {
    var tsr = textStyleRanges[i];
    alert(tsr.appliedCharacterStyle.name);
}

Does that make sense? TextStyleRanges are ranges of text that have exactly the same character attributes and styles applied.

- Mark

Known Participant
March 13, 2025

Hi Mark,

Brilliant! Thanks for your speedy help.

textStyleRanges.length gives me exactly what I was looking for.

Thanks again 🙂

m1b
Community Expert
Community Expert
March 13, 2025

Happy to help!