Textranges vs TextSelection

Adobe Community Professional ,
Feb 05, 2020

Copy link to clipboard

Copied

Hi All,

 

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

  • In the DOM(object model viewer of Extendscript) i see a textRanges property on TextFrame but when i check it will debugging i see no such property? Can a textFrame have more than one ranges or not, similarly can a textframe have more than one text selection?
  • In InDesign we have textStyleRanges i.e. a piece of text that has the same formatting applied, do we have something similar in Illustrator?

 

-Manan

Adobe Community Professional
Correct answer by DilliamWowling | Adobe Community Professional

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[0];

myTextFrame.contents = "Hello";

alert(myTextFrame.textRange.contents); // "Hello"

alert(myTextFrame.textRanges[0].contents); // "H"

alert(myTextFrame.textRanges[3].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[0];

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.

TOPICS
Scripting

Views

260

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Textranges vs TextSelection

Adobe Community Professional ,
Feb 05, 2020

Copy link to clipboard

Copied

Hi All,

 

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

  • In the DOM(object model viewer of Extendscript) i see a textRanges property on TextFrame but when i check it will debugging i see no such property? Can a textFrame have more than one ranges or not, similarly can a textframe have more than one text selection?
  • In InDesign we have textStyleRanges i.e. a piece of text that has the same formatting applied, do we have something similar in Illustrator?

 

-Manan

Adobe Community Professional
Correct answer by DilliamWowling | Adobe Community Professional

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[0];

myTextFrame.contents = "Hello";

alert(myTextFrame.textRange.contents); // "Hello"

alert(myTextFrame.textRanges[0].contents); // "H"

alert(myTextFrame.textRanges[3].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[0];

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.

TOPICS
Scripting

Views

261

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 05, 2020 0
Adobe Community Professional ,
Feb 07, 2020

Copy link to clipboard

Copied

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[0];

myTextFrame.contents = "Hello";

alert(myTextFrame.textRange.contents); // "Hello"

alert(myTextFrame.textRanges[0].contents); // "H"

alert(myTextFrame.textRanges[3].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[0];

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 07, 2020 3
Most Valuable Participant ,
Feb 07, 2020

Copy link to clipboard

Copied

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?

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 07, 2020 1
Adobe Community Professional ,
Feb 09, 2020

Copy link to clipboard

Copied

Hi  williamadowling and  pixxxel_schubser,

 

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

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 09, 2020 0
Most Valuable Participant ,
Feb 09, 2020

Copy link to clipboard

Copied

Please show an example before and after.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 09, 2020 0
Adobe Community Professional ,
Feb 11, 2020

Copy link to clipboard

Copied

Hi  pixxxel_schubser,

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

  • There
  • would be no change
  • in the
  • document
  • so there is no before after that i can exemplify

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.

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 11, 2020 0
Adobe Community Professional ,
Feb 11, 2020

Copy link to clipboard

Copied

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. =(

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 11, 2020 1
Resources