Copy link to clipboard
Copied
Hi all,
I come from the InDesign scripting world, where drawing text frames and populating them with text is relatively straightforward. I struggle doing it in Illustrator. Below, I am trying to populate the magenta info (which comes from the blue rectangle's layer name) programmatically. I'd like the text to be as large as possible and centered within its containing frame; all text should be uniform in size, and not stretched. I'm using the following code:
tf = layer.textFrames.add();
tf.contents = rects[i].name;
tf.textRange.paragraphs[0].fillColor = magenta();
tf.position = rects[i].position;
tf.height = rects[i].height;
tf.width = rects[i].width;
How do I get the contents to not stretch when I bring it in? I'd like everything to come in in a uniform size so that I can then figure out how big the text needs to be by looking at the smallest rect and determing an appropriate size based on that. Thanks.
If all you wish is to get all the text in, then do not set the width and height of the text frame - it will be automatically the same size for all the incoming items. You can still set the position of the text, and maybe do a center justification and some offsetting to put the anchor of a point text into the middle. Then you can select them all and manually (or automatically) dial in the font size that will have the largest item fit its bounds - if all are selected and their point-text anchors a
...Copy link to clipboard
Copied
You have to use area text instead of point text, try to convert the point text items to area text items: it apparently works OK with selection only, but doesn't do well with a UUID which means that it really destroys the old object and replaces it with a completely new one.
var doc = app.activeDocument;
var x = doc.textFrames.add();
alert(x.kind.toString()); // TextKind.POINTTEXT
x.contents = "I am POINT TEXT";
x.selected = true;
var storedUuid = x.uuid;
x.convertPointObjectToAreaObject();
alert(x.kind.toString()); // Still TextKind.POINTTEXT
// alert((doc.getPageItemFromUuid(storedUuid)).kind.toString()); // PARM ??
alert((doc.selection[0]).kind.toString()); // Now it's an AreaText
Copy link to clipboard
Copied
I get this with point text:
var rects1 = [];
for (var i = 0; i < app.activeDocument.pathItems.length; i++) {
rects1.push(app.activeDocument.pathItems[i]);
}
rects1.sort(function(a, b) {return a.width - b.width;});
var w = rects1[0].width;
rects1.sort(function(a, b) {return a.height - b.height;});
var h = rects1[0].height;
var rects2 = app.activeDocument.pathItems;
var magenta = app.activeDocument.swatches["CMYK Magenta"].color;
for (var i = 0; i < rects2.length; i++) {
var tf = app.activeDocument.textFrames.add();
tf.contents = rects2[i].name;
tf.textRange.fillColor = magenta;
tf.width = w;
tf.height = h;
var x = rects2[i].position[0] + (rects2[i].width - w) / 2;
var y = rects2[i].position[1] - (rects2[i].height - h) / 2;
tf.position = [x, y];
}
I don't get a good result with area text, because I can't vertically centre the text in the text frame and the horizontal scaling is messed up. convertPointObjectToAreaObject() doesn't work for me (it's probably too recent for CS6).
Copy link to clipboard
Copied
For your situation it may be viable to create 'meta' bounds by creating a point-text copy and doing an outline on this, then storing some numbers having to do with the bounding box in a meta-spot such as notes or scripting tag.
Copy link to clipboard
Copied
If all you wish is to get all the text in, then do not set the width and height of the text frame - it will be automatically the same size for all the incoming items. You can still set the position of the text, and maybe do a center justification and some offsetting to put the anchor of a point text into the middle. Then you can select them all and manually (or automatically) dial in the font size that will have the largest item fit its bounds - if all are selected and their point-text anchors are somewhere within the rectangles properly, they should all scale down at once to fit your needs (pun intended).
Copy link to clipboard
Copied
That does sound like a better approach. Let me give that a shot. Illustrator text frames are so unintuitive for me, an ID guy. Thanks.
Copy link to clipboard
Copied
Speaking as an Illustrator guy... they don't make much sense to me either. 😉
Copy link to clipboard
Copied
Having ensured the text frame is area text, set the height and width of the path that defines its border:
tf.textPath.height = rects[i].height;
tf.textPath.width = rects[i].width;