Skip to main content
sinrise
Known Participant
October 18, 2011
Answered

Selecting Text based on characterAttributes

  • October 18, 2011
  • 1 reply
  • 1399 views

This is a newbie question and the Illustrator Scripting guide is terrible. I'm struggling with the basics.

All I want to do is select all text with a horizontal scale of 94.62%.

Here's what I'm trying:

var myDoc = app.activeDocument

var myText = myDoc.textFrames

var properNames = myText.characterAttributes.horizontalScale[94.62];

properNames.selected

but I get a 'no such element' error.

Of course, the correct code would be great but I'm really hoping for some help understanding why my code isn't working. I really need to understand this stuff. TIA!

This topic has been closed for replies.
Correct answer CarlosCanto

Thanks so much! It works beautifully. This simple little bit of code will

end up saving me hours and hours over work.

I was actually very close. I didn't add the .toFixed (2) part. What does

that part do?

I actually had the basic logic of the if statement done except I was only

using = instead of ==. Without the second = it takes over a minute and ends

up selecting all the text.

-Ivan


you're welcome,

toFixed(2) forces the number to 2 decimal places. I had to use it because Illustrator was returning 94.619999999999999 and I wasn't getting any matches.

a = 10 // asign 10 to a

a == 10 // compare 10 to a

1 reply

CarlosCanto
Community Expert
Community Expert
October 18, 2011

here's why

var myText = myDoc.textFrames

myText represents "all" textFrames, in order to do something with each textFrames you'll need to call each frame separately with something like "var myText = myDoc.textFrames[0]" for the first text frame.

myText.characterAttributes

TextFrames don't have characterAttributes property, how do you know? go to the documentation and look at the TextFrame properties, it is not there...but they do have a "textRange" property....now go to TextRange properties and...bingo...you see characterAttributes there, so...do something like "myText.textRange.characterAttributes"

horizontalScale[94.62];

horizontalScale is not an array, so set it like this, "horizontalScale = 94.62", or read it with "var hs = myText.textRange.characterAttributes.horizontalScale;"

- loop thru all textFrames

- read the scale

- select accordingly

sinrise
sinriseAuthor
Known Participant
October 18, 2011

Thanks, Carlos! That helps me out a lot. I'm still studying. For me, having

a practical application is pretty much the only way I'll retain the

information. So, now I'm off to figure out how to loop through the

textFrames. Advice is more than welcome.

CarlosCanto
Community Expert
Community Expert
October 19, 2011

practice with the samples in the documentation,

for (i=0; i<myDoc.textFrames.length; i++)

     {

          var myText = myDoc.textFrames;

          var hs = myText.textRange.characterAttributes.horizontalScale;

          alert ("scale = " + hs);

         

          // check the scale

          // select if equals 94.62

     }

I'm not giving you the answer yet, try to work it out first