Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
2

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

Community Beginner ,
Jan 31, 2024 Jan 31, 2024

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:

voscekm_0-1706700785278.pngexpand image

 

Into this:

voscekm_1-1706700898886.pngexpand image

 

Any scripts out there?

 

Thank you

 

TOPICS
Scripting

Views

258
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Guide , Jan 31, 2024 Jan 31, 2024

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].
...

Votes

Translate
Guide , Feb 09, 2024 Feb 09, 2024

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), 
       
...

Votes

Translate
Guide ,
Jan 31, 2024 Jan 31, 2024

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

Works like magic! Thank you very much.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 09, 2024 Feb 09, 2024

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 12, 2024 Feb 12, 2024

Copy link to clipboard

Copied

LATEST

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!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines