Just to illustrate my last point:
Here an example with a paragraph formatted with only one GREP Style, letter "b" should change color to Cyan. If you select letter b only, the textStyleRange includes letter "b" (no surprise here) but also all the characters to the end!
Code:
var mySelection = app.selection[0];
var myTextStyleRanges = mySelection.texts[0].textStyleRanges.everyItem().getElements(); for(var n=0;n<myTextStyleRanges.length;n++){ $.writeln(myTextStyleRanges[n].texts[0].contents); $.writeln(myTextStyleRanges[n].texts[0].fillColor.name); };
Result in the console:
bcccddddd
C=100 M=0 Y=0 K=0
Which is misleading…
Further we could try to get the fill Color for all characters of the textStyleRange and ask them about their fillColor.name:
var mySelection = app.selection[0];
var myTextStyleRanges = mySelection.texts[0].textStyleRanges.everyItem().getElements(); for(var n=0;n<myTextStyleRanges.length;n++){ $.writeln("Contents of Range:"+"\r"+myTextStyleRanges[n].texts[0].contents); $.writeln("fillColor.name of Range:"+"\r"+myTextStyleRanges[n].texts[0].fillColor.name); var allCharsInRange = myTextStyleRanges[n].texts[0].characters.everyItem().getElements(); for(var c=0;c<allCharsInRange.length;c++){ $.writeln("Index number of loop: "+c+"\t"+allCharsInRange[c].contents+"\t"+allCharsInRange[c].fillColor.name); }; };
The result for my example would be:
First column of result: index number of for loop (0-8), there are 9 characters in the one and only range
Second column: contents of the single character
Third column: fillColor.name of the character
Contents of Range:
bcccddddd
fillColor.name of Range:
C=100 M=0 Y=0 K=0
Index number of loop: 0 b C=100 M=0 Y=0 K=0
Index number of loop: 1 c Black
Index number of loop: 2 c Black
Index number of loop: 3 c Black
Index number of loop: 4 d Black
Index number of loop: 5 d Black
Index number of loop: 6 d Black
Index number of loop: 7 d Black
Index number of loop: 8 d Black
Uwe