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.

Robert at ID-Tasker
Legend
March 14, 2025

Hi Mark,

 

Brilliant!!! 🙂

Thanks for your help and showing me a different way to approach it.

Your solution works great and does what I was trying to do 🙂

 

I'm going to try and take it one step further and draw a triangle just off the page to indicate found instances but,

what you've done will already be a big help!

 

Yes, I'm familiar with Grep thanks. I use it every week in some of my find/replace presets but previously, BBEdit!

 

Thanks again for all your help.

 

Cheers Mark 🙂

 

 

P.S. Just curious, would my approach of checking the textStyleRange contents have worked or could you forsee issues?


quote

I'm going to try and take it one step further and draw a triangle just off the page to indicate found instances 

 

By @Nick37129562ryen

 

In that case - there is an easier solution - Anchor an object just before your word - or after - with applied ObjectStyle, so you can easily reposition / format all those "highlighters".

 

Another bonus - they'll flow with the text.