• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Textranges vs TextSelection

Community Expert ,
Feb 05, 2020 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

TOPICS
Scripting

Views

1.3K

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
community guidelines

correct answers 2 Correct answers

Community Expert , Feb 07, 2020 Feb 07, 2020

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 ac

...

Votes

Translate

Translate
Community Expert , Feb 11, 2020 Feb 11, 2020

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 pro

...

Votes

Translate

Translate
Adobe
Community Expert ,
Feb 07, 2020 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.

Votes

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
community guidelines
Community Expert ,
Feb 07, 2020 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?

 

 

Votes

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
community guidelines
Community Expert ,
Feb 09, 2020 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

Votes

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
community guidelines
Community Expert ,
Feb 09, 2020 Feb 09, 2020

Copy link to clipboard

Copied

Please show an example before and after.

Votes

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
community guidelines
Community Expert ,
Feb 11, 2020 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

Votes

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
community guidelines
Community Expert ,
Feb 11, 2020 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. =(

Votes

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
community guidelines
Guide ,
Dec 28, 2020 Dec 28, 2020

Copy link to clipboard

Copied

Hi everybody

 

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[0];
myTextFrame.contents = "Hello";
alert( myTextFrame.textRanges[0].contents );  // H
alert( myTextFrame.characters[0].contents );  // H
alert( myTextFrame.textRanges[0] == myTextFrame.characters[0] );  // false

Thanks in advance. 

 

Votes

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
community guidelines
Community Expert ,
Dec 28, 2020 Dec 28, 2020

Copy link to clipboard

Copied

@femkeblanco 

 

I don't know the answer to your question for sure, but it does appear that textRanges[0] and characters[0] 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[0] object can only be a single character.

Votes

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
community guidelines
Guide ,
Dec 28, 2020 Dec 28, 2020

Copy link to clipboard

Copied

LATEST

Thanks. Yes, in JavaScript two objects are not equal, even if they possess the same properties and values, unless they refer to the same object. 

Votes

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
community guidelines