Copy link to clipboard
Copied
Lets say we have some text:
and there are some different formattings - doesn't matter if local or CharStyles.
Now, lets iterate through TextStyleRanges collection of the SELECTED text.
Above example have 4x TSRs:
2nd and 3rd TSRs can be correctly referenced & compared (I'm checking indexes of 1st and last characters) but for simplification:
myText = myText.TextStyleRanges.Item(1).Texts.Item(1)
BUT - returned parent TSR for the 1st text - "NeYxEcTÓW"- even when I've not selected whole TSR - is the same as myText - in this case, parent TSR should be the same as the whole paragraph - "lOmY PfDiIcŁ KoNeYxEcTÓW" - so in the screen above it should be "Text" instead of "TSR" in the TYPE column.
parent of the 4th TSR is returned correctly as "kxlinnacipm" and I have "Text" in the TYPE column.
Can someone verify if you have the same problem - that parent TSR is returned correctly when you select "start" of the TSR - but incorrectly when you select "end" of the TSR.
Parent Paragraph - myText.Paragraphs.Item(1).Texts.Item(1) - doesn't have this problem.
Copy link to clipboard
Copied
> myText.Paragraphs.Item(1).Texts.Item(1)
This should return the first text-style range as a text object. In JS it works like that just as in VBS, and I think that that's correct.
Copy link to clipboard
Copied
I'm not talking about Paragraph - I'm talking about TextStyleRanges.
Your quote will return whole Paragraph as a Text object - not a TSR.
Copy link to clipboard
Copied
Hi @Robert at ID-Tasker , Did you double check the textStyleRange length to make sure it really is 4? With this example:
I get this with JS:
var s = app.activeDocument.selection[0]
var tr = s.textStyleRanges;
$.writeln(tr.length) //returns 7
$.writeln(tr[0].contents) //returns Apples
$.writeln(tr[1].contents) //returns ,
$.writeln(tr[-1].contents) //returns Lemons
$.writeln(tr[0].parent.contents) //returns Otatem ium cuscieni ut fugiam et etur? Apples, Oranges, Limes, Lemons—Pidunditibus mos dus modit fugiatquosam.
Copy link to clipboard
Copied
It works fine when you select WHOLE TSR - try to select from "ples" to "lemo".
And Parent for TR[0] is Story - no problem there.
Copy link to clipboard
Copied
Robert is right that not all is well: when you select "ples" to "lem" in Rob's example, then
app.selection[0].textStyleRanges[-1].contents
returns lemons, it should return lem.
app.selection[0].textStyleRanges[0].contents
returns ples, which is correct.
Copy link to clipboard
Copied
returns lemons, it should return lem.
At first I thought the same, but tr[-1] returns [object TextStyleRange] not [object Text], so its contents would be the text to the end of the style range—no?
var s = app.activeDocument.selection[0]
var tr = s.textStyleRanges;
$.writeln(tr.length) //returns 7
$.writeln(tr[0].contents) //returns ples
$.writeln(tr[1].contents) //returns ,
$.writeln(tr[-1]) //returns [object TextStyleRange]
$.writeln(tr[-1].contents) //returns Lemons
$.writeln(tr[0].parent.contents) //returns Otatem ium cuscieni ut fugiam et etur? Apples, Oranges, Limes, Lemons—Pidunditibus mos dus modit fugiatquosam.
$.writeln(tr[0].paragraphs[0]) // returns [object Paragraph]
Copy link to clipboard
Copied
Yes, and that's correct for tr[-1] - but the problem is with tr[0] - it returns only part of the TSR - the rest/selection - when it should also return what is before the selection.
Copy link to clipboard
Copied
Also, I’m checking in ID 16.4, so this isn’t new.
Copy link to clipboard
Copied
Please check tr[0].
Copy link to clipboard
Copied
This might be a work around:
var s = app.activeDocument.selection[0]
var tr = s.textStyleRanges;
var ls = s.characters[-1];
var lr = tr[-1].characters[0]
$.writeln(tr[0].contents) //returns ples
$.writeln(tr[-1].characters.itemByRange(lr,ls).contents) //returns Lemo
Copy link to clipboard
Copied
Exactly 🙂
So the bug is confirmed 😞
Copy link to clipboard
Copied
Hi,
To summarize, a TextStyleRange is not what we thought it was 😞
Given a single Text object that I'll suppose having a consistent style, the associated text style range is left-bounded (it won't go before the first targeted character) but it runs as far as needed on the right:
And it only ends when something (explicitly) changes in text styling, so it can even cross the paragraphs:
Whether it is a “bug” or a strangely asymmetrical definition, I don't know, but that's the way InDesign text style ranges are formed and reported at the DOM level.
There are cases where (1) we want to get the missing part (to the left) as stressed by @Robert at ID-Tasker, in order to retrieve a maximal, consistent range (no matter the input Text). There are also cases where (2) we do not want the range to expand (to the right) beyond the input Text.
Case (2) is easier to manage, since we know the actual length of the input (myText.length or mySel.texts[0].length). In case (1) some extra steps are required to identify the actual beginning of the range (based on parent word, line, paragraph, column, frame, story—relative to the Text under consideration.)
Best,
Marc
Copy link to clipboard
Copied
But when you get parent Paragraph - it is always returned correctly - so in case of parent TSR - it's a bug 😞
It should always return parent in full - selection shouldn't matter in any way.
TSR is a range of text with exactly the same formatting - no confusion here - at least not for me? So it shouldn't be "bounded" to any side nor should "expand" or "contract" - it should be returned in full - like any other "parent".
When you need a reference to a parent Spread - would it be OK to get partial reference - left or right page - depending on which side, source object is placed - instead of the Spread?
I know there is no direct reference to a parent Spread - you need to get parent Page and then parent Spread of this Page - so technically, it's a bit pointless example 😉 but I hope you get my point 😉
Copy link to clipboard
Copied
I totally agree with you, Robert. The fact is just that TSR haven't been designed as “parent” components. When processed from an arbitrary selection they behave as left-bounded runs, and that's obviously an issue.
Marc
Copy link to clipboard
Copied
> At first I thought the same, but tr[-1] returns [object TextStyleRange] not [object Text], so its contents would be the text to the end of the style range—no?
Still, tr[0] returns [object TextStyleRange] and its contents returns ples. So that's not consistent.
Copy link to clipboard
Copied
Just checked in CS6 and it the same, so it’s been that way for awhile
Copy link to clipboard
Copied
Thanks @Robert at ID-Tasker for looking into this. I strongly suggest next time deliberately using a *very* simple and clean example, like @rob day's Apple and Lemons example. Your example was difficult to parse and might have confused some of us. 🙂
Copy link to clipboard
Copied
Probably 😉 noted 😉
Copy link to clipboard
Copied
Hi together,
it's a bit off topic perhaps, but while we are at textStyleRanges now consider the following:
Test document attached:
TextStyleRanges-length-GREP-Style-vs-CharacterStyleApplied-2023.indd
Regards,
Uwe Laubender
( Adobe Community Expert )
Copy link to clipboard
Copied
Because formatting applied through a GREP Style is kind of "virtual" 😞
Pretty sure Drop Caps and Nested Styles behave the same.
Copy link to clipboard
Copied
That's an interesting little gotcha! Would be ideal if length was 3 in both cases. Thanks for info.
Copy link to clipboard
Copied
but while we are at textStyleRanges
And the Style in textStyleRanges doesn’t refer to paragraph or character styles, any change to a character property starts a new range:
Copy link to clipboard
Copied
Yes, ANY kind of formatting that is different to a surrounding area - will create new TSR - that's the point of TSRs 😉