I am looking into the scripting DOM with respect to text handling and i see some properties that i am not clear about. Mentioned below are my queries
What version of illustrator are you using? Are you using ESTK for debugging? I can see textRange in my data browser panel nested inside ..
First off, i'll answer the question in your title. textSelection merely represents a textRange of characters that have been selected (highlighted) in the UI. For a lot of reasons, I don't use this particular functionality. It seems more reliable to me to make my own textRange programattically (which we'll look at below).
But, more to the question you were actually asking... TextFrame.textRanges is an array of child textRange objects in the given textFrame. By default, the textRange property of a textFrame represents all characters in the text frame, and a child textRange (an element of the textFrame.textRanges array) represents a single character.. So for example, if your textFrame has the contents "Hello", then you'll find the following:
var myTextFrame = app.activeDocument.textFrames; myTextFrame.contents = "Hello"; alert(myTextFrame.textRange.contents); // "Hello" alert(myTextFrame.textRanges.contents); // "H" alert(myTextFrame.textRanges.contents); // "l"
But, the real functionality comes in here.. You can also set your own custom textRange (by number of characters, or perhaps by some kind of regex or indexOf etc) by manipulating the "start" and "end" properties of the TextFrame.textRange object. So let's say we wanted to change our textRange to begin at the 3rd character of my textFrame. just change the start property to 2, like so:
var myTextFrame = app.activeDocument.textFrames; var myRange = myTextFrame.textRange; //now you can edit the textRange and view it's properties without breaking your textFrame //lets just get the second and third character from myTextFrame as a textRange myRange.start = 1; myRange.end = 2 alert(myRange.contents); //you can still edit the range to get different portions as well myRange.start = 3; myRange.end = 5; alert(myRange.contents);
and finally, to your last question. I don't have any experience in coding for InDesign, so I'm not sure if there's an analog to the textStyleRanges property you mentioned. But i do know that the appearance/style of text in illustrator is stored here: TextFrame.textRange.characterAttributes;
You can adjust the character attributes of the entire textFrame by accessing the TextFrame.textRange.character attributes property, or you can adjust the character attributes of each individual character (or any specified range) by accessing the child elements individually or predefining your own textRange ahead of time as I did above.
I hope this was helpful.
Hi @Manan Joshi,
IMO the property textSelection in Illustrator text frames is not really useful, but hard to describe.
That's why: textSelection in textFrames is a [textRange] and is (Read Only) and that's all.
Much more useful is textSelection as property of edittext in a dialog (Script-UI)
Do you actually have a project? Or do you ask for interest only?
Many thanks for sharing your insights on my queries, i am much more informed now. Sorry i am not sure how i was finding the textRanges property as being absent, but now i see it behaving exactly as you guys mentioned and also understood the textselection use case.
I am working on a project to segregate the contents of a textframe into group ofcontinous texts that has similar style properties. This is readily available in InDesign via the textstyleranges property but in Illustrator i don't see any such classification of text based on its styling,.We do have characterattributes and paragraphattributes that can be drilled down to each character. So my solution is to iterate each character and match its character/paragraph attibute with its next character and see if these match or not to create a collection of similarly styled characters. The issue is i think this would be a computationally intensive task. Do you guys have any better ideas to accomplish this or this is the only way forward.
Thanks again for your time
Please show an example before and after.
There would be no change in the document so there is no before after that i can exemplify. However for an example. Consider the text in the first line. The characters in the first word There has the same formatting and hence they will form a textstylerange, the "would be no change" fragment is italic differing from the styling of the text before it will form the next textstylerange. So what i want to get is an array of these textstylerange, which for the line in question would be
In InDesign we have an api collection that gives us this, in Illustrator i could not find anything so i decided to iterate the text character wise and then create a collection. This would ceratinly be a slow process, so i was curious if there is some other way to approach this.
InDesign gets lots of shiny tools that are not available to illustrator unfortunately.. =(
It genuinely seems as though Adobe simply does not care about people who automate illustrator. It's clear in the documentation, the haphazard way the API was compiled (seemingly from many many different inconsistent sources), and the absolute and complete silence in the face of hundreds of well reported, commonly seen bugs..
Unfortunatley, i think that your iteration of each character and checking it's properties is the best way to do what you're trying to do. =(
Can anyone tell me what the difference is between textRanges[i] and characters[i]? As far as I can tell, they seem to have the same properties, but are not the same. And why have both?
var myTextFrame = app.activeDocument.textFrames; myTextFrame.contents = "Hello"; alert( myTextFrame.textRanges.contents ); // H alert( myTextFrame.characters.contents ); // H alert( myTextFrame.textRanges == myTextFrame.characters ); // false
Thanks in advance.
I don't know the answer to your question for sure, but it does appear that textRanges and characters are identical objects (although, i don't think you can compare 2 objects with a double equal operator because i think that checks the memory address of the object, not the contents. looks like these 2 objects have the same contents, but different addresses?).
My guess as to why both exist is that textRange can be more than one character. Like you could set your textRange to a single character or multiple and then manipulate the whole textRange. I imagine the characters object can only be a single character.