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
  • 1355 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,

Just another quick question, please.

Is it possible to get the page position coordinates of a word, that may have more than one style applied to it, from my initial selection?

Thanks.

Known Participant
March 14, 2025

@Nick37129562ryen

 

Do you have a list of words you want to find - or only formatting?

 


Only formatting, thanks.

I already have a Grep find/replace I can use to search for a list of words.