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

[Q] How to find the with of a string that is smaller than the width of an Illustrator textFrame?

Explorer ,
Sep 29, 2023 Sep 29, 2023

I have a number of textFrames (area text), most of which contain a single line of text which is narrower than the width of the textFrame.

tomd75724979_0-1695994370831.pngexpand image

 

 

I'm trying to get the width of the string in points and I'm stumped... I tried:

textPath.width

textPath.pathPoints[1].anchor[0]

 

What is the right way of doing this?

 

Any help greatly appreciated.

 

// Tom

 

 

TOPICS
How-to , Scripting , Type
665
Translate
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 1 Correct answer

Community Expert , Oct 01, 2023 Oct 01, 2023

Hi Tom, I can replicate your issue, I checked my notes and in fact I had noticed the reference gets lost after converting to PointText but couldn't remember. Since the dup text frame gets selected after duplicating, we'll put this selection right back into our variable.

 

insert these two lines after duplicating your text frame

app.redraw();
vPointTextCopy = selection[0];

 

Let me know if that works, if not we'll look into other workarounds

Translate
Adobe
Community Expert ,
Sep 29, 2023 Sep 29, 2023

you could make a copy of the Area text and convert it to point text. The bounding box will shrink to the size of the text,, then just get the point text width

Translate
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
Explorer ,
Sep 30, 2023 Sep 30, 2023

Hi Carlos,

 

Many thanks. This sounds like a good plan but I have problems implementing it.

 

// convert to point text
var myPointText = myDocument.textFrames[i].duplicate();
myPointText.convertAreaObjectToPointObject();

 

 

Contrary to what the docs say, the call convertareaobjecttopointobject returns nil rather than a point text object. And it does not seem to convert the current area text object either.

 

Any ideas?

 

 

 

 

Translate
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 Expert ,
Sep 30, 2023 Sep 30, 2023

Hi Tom, convertAreaObjectToPointObject() doesn't return anything, it just converts the duplicate to point text

 

your script works fine here, is it not working for you? do you get any errors?

 

Translate
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
Explorer ,
Oct 01, 2023 Oct 01, 2023

Hi Carlos,

 

Thank you for clarifying, I got confused by the documentation which seems to suggest a return type:

tomd75724979_0-1696198499876.pngexpand image

 

I can now convert area text to point text but I still have problems picking up the width (and height if that is possible) of the point text.

 

My artboard looks like this when the text is still area text:

tomd75724979_1-1696198603496.pngexpand image

My layer structure looks like this:

tomd75724979_2-1696198642634.pngexpand image

And my code like this. I get an error when I try to remove the duplicate which I converted to point text. And the rectangles don't pick up the new size but stick to the old size.

 

for (var i = 0; i < myDocument.layers["Text"].pageItems.length; i++)
{

var vParent = myDocument.layers["Text"].pageItems[i]; // get the elements of the group
var vNumbers = vParent.pageItems.length; // numbers of elements

for ( var j = vNumbers - 1; j >= 0 ; j-- ) {


var vChild = vParent.pageItems[j];
var vPointTextCopy = vChild.duplicate();
var gB = vPointTextCopy.geometricBounds; // left, top, right, bottom
var w = Math.abs(gB[2] - gB[0]);
var h = Math.abs(gB[3] - gB[1]);
var top = gB[1];
var left = gB[0];

app.redraw();

var rect = myDocument.layers["TestRectLayer"].pathItems.rectangle(top, left, w, h); // de oorsprong ligt linksonder
rect.name = "TestRect";
rect.stroked = true;
rect.filled = false;
rect.strokeWidth = 3;
rect.strokeColor = redColor;

vPointTextCopy.remove(); // throws an error
}
}

 

So I get:

 

tomd75724979_3-1696199415472.pngexpand image

 

And I'm stuck with the point text objects which I can't remove. It appears as if the pointers still point to the original objects.

 

Any help greatly appreciated.

 

// Tom

 

Translate
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 Expert ,
Oct 01, 2023 Oct 01, 2023

Hi Tom, I can replicate your issue, I checked my notes and in fact I had noticed the reference gets lost after converting to PointText but couldn't remember. Since the dup text frame gets selected after duplicating, we'll put this selection right back into our variable.

 

insert these two lines after duplicating your text frame

app.redraw();
vPointTextCopy = selection[0];

 

Let me know if that works, if not we'll look into other workarounds

Translate
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
Explorer ,
Oct 02, 2023 Oct 02, 2023

Thank you so much, Carlos. This works! BTW, is this the most efficient way to set the dimensions of a rectangle from geometricbounds? I find the distinction between [x1, y1, x2, y2] and [top, left, bottom, right] quite confusing.

 

tomd75724979_0-1696240126209.pngexpand image

 

    for ( var j = vNumbers - 1; j >= 0 ; j-- ) {

        var vChild = vParent.pageItems[j];
        vPointTextCopy = vChild.duplicate();
        vPointTextCopy.convertAreaObjectToPointObject();
        app.redraw();
        vPointTextCopy = selection[0]; // this is a hack to recover the lost pointer

      
        var gB = vPointTextCopy.geometricBounds; // left, top, right, bottom
        var w = Math.abs(gB[2] - gB[0]);
        var h = Math.abs(gB[3] - gB[1]);
        var top = gB[1];
        var left = gB[0];


        var rect = myDocument.layers["TestRectLayer"].pathItems.rectangle(top, left, w, h); // de oorsprong ligt linksonder
        rect.name = "TestRect";
        rect.stroked = true;
        rect.filled = false;
        rect.strokeWidth = 3; 
        rect.strokeColor = redColor;
    }

 

Translate
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 ,
Oct 02, 2023 Oct 02, 2023
geometricBounds = [left, top, right, bottom]
 
Or
femkeblanco_0-1696245131497.pngexpand image
Translate
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 Expert ,
Oct 02, 2023 Oct 02, 2023

yeah, that's the correct way, bounds are a little confusing at first but easy once you get the hang of it. 

 

femke's diagram should help visualizing it

Translate
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
Explorer ,
Oct 03, 2023 Oct 03, 2023

Thank you, both. What I actually meant is the conversion from geometricBounds to Rect and whether this is necessary in the first place. Is it not possible to initialise a Rect with a geometricBounds array? Or get the Rect of a Point Type Textframe?

 

What I was hoping for is:

myDocument.layers["TestRectLayer"].pathItems.rectangle(vPointTextCopy.rect);

 

or

 

myDocument.layers["TestRectLayer"].pathItems.rectangle(vPointTextCopy.geometricBounds);

 

It seems funny to have two data structures which contain essentially the same information without standard conversion functions.

 

Translate
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 Expert ,
Oct 03, 2023 Oct 03, 2023
LATEST

I see what you mean, and no, unfortunately we can't pass a bounding array to the Rectangle function

 

but you could create your own function if you need to make this often

Translate
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