Skip to main content
Inspiring
December 18, 2013
Question

Get text selection and properties

  • December 18, 2013
  • 2 replies
  • 1484 views

Hi All,

I need to get the text selection in my active document so that I can access the properties of selected text like paragraphs, font styles, colors etc.

I wrote the following script to get text selection,

var objDoc = app.activeDocument;

var objText = app.selection[0];

This gives me objText as a Text object. When I try to access paragraphs in this text, I get the full contents of the para, not just the ones in the selection. I do not want this. I want to access properties in just the text selection.

How can I achieve this?

This topic has been closed for replies.

2 replies

Green4ever
Inspiring
December 19, 2013

objText.contents will give you the contents of the selection not the whole content of the paragraph.

If you want to get the properties of the selection, try something like objText.appliedParagraphStyle.

Warning: If you try to get the properties like appliedCharacterStyle, font-size, and font-family when there is multiple textStyleRanges it will not work properly. It will only give you the properties of the first textStyleRange (I hope).

~Green4ever

Community Expert
December 19, 2013

@All – let's do an example of a selection with a screenshot (always best to illustrate the problem):

Simple example, some characters selected with different formatting (in this case the fillColor property):

In total 5 textStyleRanges in the text frame.
Two textStyleRanges in the selection of the same frame below.

If you want to ask for the fillColor used in the 2 textStyleRanges of the selection, you can do it like that:

var mySelection = app.selection[0];

var myTextStyleRanges = mySelection.texts[0].textStyleRanges.everyItem().getElements();

for(var n=0;n<myTextStyleRanges.length;n++){

    $.writeln(myTextStyleRanges.fillColor.name);

    };

The result of my example as written to the JavaScript console of the ESTK (ExtendScript Toolkit):

"C=75 M=5 Y=100 K=0" for the first textStyleRange in the selection

"C=100 M=0 Y=0 K=0" for the second textStyleRange in the selection

But like the example with asking for paragraphs in a selection, the same goes for textStyleRanges:
You are asking for the whole object, you'll get the whole object.

And if you would ask for the "contents" (the not formatted string representation of a texts object), in the case of textStyleRanges in my example like that

myTextStyleRanges.texts[0].contents

you would get the following results:

"ij" for the first textStyleRange in the selection

"klmn" for the second textStyleRange in the selection

However, the contents of the selection in my example would be:

"ijkl"

Uwe

Community Expert
December 19, 2013

I just want to add for the OP: textStyleRanges will change every time the formatting of texts is changed. In the case of subtle changes at first glance to the text you cannot see them when looking in the UI. It could be anything you could format a text with.

So, if you are after some destinct properties (out of maybe 300), you may need other methods to be effective.

And another important thing: in the case your paragraphStyle includes GREP Style definitions, the method using textStyleRanges will not work as expected.

Example:

If you then ask how many textStyleRanges your selection will consist of,

the result will be: 1

Asking for the fillColor of that range you'll get in my example:

"C=100 M=0 Y=0 K=0"

However, if you ask your selection for the fillColor.name of every individual character with

var mySelection = app.selection[0];

var myCharacters = mySelection.texts[0].characters.everyItem().getElements();

for(var n=0;n<myCharacters.length;n++){

    $.writeln(myCharacters.fillColor.name);

    };

you'll get the following answer:

"C=100 M=0 Y=0 K=0" for character 1 of your selection

"C=0 M=100 Y=0 K=0" for character 2 of your selection

Uwe

Jump_Over
Legend
December 18, 2013

Hi,

Everything you wrote is possible to access.

If your result is wrong = just paste a code which you expect as correct one.

Jarek