Skip to main content
Participant
Jan 31, 2024 11:35 AM UTC
Answered

Update name of the object based on text located in that object?

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

 

This topic is closed to new replies. Start a new post to keep the conversation going.
femkeblanco
femkeblancoCorrect answer
Legend
Feb 9, 2024 8:58 PM UTC

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;
}

4 replies

femkeblanco
femkeblancoCorrect answer
Legend
Feb 9, 2024 8:58 PM UTC

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;
}
Translate
voscekmAuthor
Participant
Feb 12, 2024 3:48 PM UTC

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!

Translate
femkeblanco
Legend
Jan 31, 2024 1:35 PM UTC

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;
        }
    }
}
Translate
voscekmAuthor
Participant
Feb 1, 2024 9:54 AM UTC

Works like magic! Thank you very much.

Translate