Copy link to clipboard
Copied
Looking for a way (hopefully in a mass) to update name of the object (<Rectangle>) to mach the text that is located inside of that rectangle.
Basically to go form this:
Into this:
Any scripts out there?
Thank you
2 Correct answers
Untested.
var doc = app.activeDocument;
var items = doc.pathItems;
var text = doc.textFrames;
for (var i = 0; i < items.length; i++) {
var itemBounds = items[i].geometricBounds;
for (var j = text.length - 1; j > -1; j--) {
var textBounds = text[j].geometricBounds;
if ((textBounds[0] > itemBounds[0] && textBounds[0] < itemBounds[2]) &&
(textBounds[1] < itemBounds[1] && textBounds[1] > itemBounds[3])) {
items[i].name = text[j].contents;
text[j].
...
As requested, try this for polygons other than rectangles. This should work for the text frame whose centre lies within the path (without depending on the bounding boxes).
var doc = app.activeDocument;
var items = doc.pathItems;
var text = doc.textFrames;
for (var i = items.length - 1; i > -1; i--) {
for (var j = text.length - 1; j > -1; j--) {
var textBounds = text[j].geometricBounds;
var centre = [
textBounds[0] + ((textBounds[2] - textBounds[0]) / 2),
...
Explore related tutorials & articles
Copy link to clipboard
Copied
Untested.
var doc = app.activeDocument;
var items = doc.pathItems;
var text = doc.textFrames;
for (var i = 0; i < items.length; i++) {
var itemBounds = items[i].geometricBounds;
for (var j = text.length - 1; j > -1; j--) {
var textBounds = text[j].geometricBounds;
if ((textBounds[0] > itemBounds[0] && textBounds[0] < itemBounds[2]) &&
(textBounds[1] < itemBounds[1] && textBounds[1] > itemBounds[3])) {
items[i].name = text[j].contents;
text[j].remove();
continue;
}
}
}
Copy link to clipboard
Copied
Works like magic! Thank you very much.
Copy link to clipboard
Copied
As requested, try this for polygons other than rectangles. This should work for the text frame whose centre lies within the path (without depending on the bounding boxes).
var doc = app.activeDocument;
var items = doc.pathItems;
var text = doc.textFrames;
for (var i = items.length - 1; i > -1; i--) {
for (var j = text.length - 1; j > -1; j--) {
var textBounds = text[j].geometricBounds;
var centre = [
textBounds[0] + ((textBounds[2] - textBounds[0]) / 2),
textBounds[1] + ((textBounds[3] - textBounds[1]) / 2)
];
if (pointIsInPoly(items[i].pathPoints, centre)) {
items[i].name = text[j].contents;
text[j].remove();
continue;
}
}
}
// Jonas Raoni Soares Silva
// http://jsfromhell.com/math/is-point-in-poly
function pointIsInPoly(poly, pt){
for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
((poly[i].anchor[1] <= pt[1] && pt[1] < poly[j].anchor[1]) ||
(poly[j].anchor[1] <= pt[1] && pt[1] < poly[i].anchor[1]))
&&
(pt[0] <
(poly[j].anchor[0] - poly[i].anchor[0]) * (pt[1] - poly[i].anchor[1]) /
(poly[j].anchor[1] - poly[i].anchor[1]) + poly[i].anchor[0])
&&
(c = !c);
return c;
}
Copy link to clipboard
Copied
Works really well with all different shapes. Thank you very much for your help. Both answers are correct, just use for both could be slightly altered. Amazing job again!

