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

illustrator javascript for "Identify overset text in a text frame and reduce the font size"

Community Beginner ,
Aug 07, 2017 Aug 07, 2017

Copy link to clipboard

Copied

HI All

I have a illustrator document contain lot of  text frames. Some of the text frames content over-set text.( half portion of text is hidden)

I already using the java script to change the text frame height I have attached script and screen shot for your reference.

image.JPG

var includeExtraLines = 0.5;

if(documents.length > 0) {

    doc = activeDocument;

    mySelection = activeDocument.selection;

    // If there are enough to process

    if (mySelection instanceof Array)

    {

        // For each of the selected items

        for(i=0; i<mySelection.length; i++) {

            // That are textFrames

            if (mySelection.typename == "TextFrame" && mySelection.kind == TextType.AREATEXT ) {

                obj = mySelection;

               

                // We only want to do this on rectangular text areas

                // TODO: Take care of rotation issues from MakePointType script

                if( obj.textPath.pathPoints.length == 4 ) {

                    objTop = obj.top;

                    objLeft = obj.left;

                   

                    // Make the new point type object and locate it

                    // Make sure the new object is in the same Z stacking order as the original

                    copy1 = obj.duplicate(obj, ElementPlacement.PLACEBEFORE);

                    //copy1.move(obj, ElementPlacement.PLACEBEFORE);

                    // now make the text box much bigger, but not absurdly big

                    // TODO: This could be better approximated by itterating thru all the WORDS in the textFrame and

                    // comparing it to all the WORDS in each of the visible text LINES. Then apply the difference / total words to the scaling

                    if( copy1.height * 10 < 2000 ) {

                        copy1.textPath.height = copy1.height * 10;

                    } else {

                        copy1.textPath.height = 2000;

                    }

               

                    howManyLines = copy1.lines.length;

                    outlineObject = copy1.duplicate();

                    outlineObject = outlineObject.createOutline();

                   

                    targetHeight = outlineObject.height + includeExtraLines * (outlineObject.height / howManyLines );

                    // Now assign y-axis depth of the point text to the area text box

                    rect = obj.parent.pathItems.rectangle(copy1.textPath.top, copy1.textPath.left, obj.width, targetHeight);

                    copy2 = obj.parent.textFrames.areaText(rect);

                    copy2.selected = true;

                    rect.selected = true;

                    // Always delete these intermediate objects

                    outlineObject.remove();

                    copy1.remove();

                   

                    // Now take care of the end and original objects

                    obj.textRange.duplicate(copy2);   

                    obj.remove();   

                }

            }

        }

    }

}

But I need to reduce the overset  text frame font size.

Please advise me

Thanks in advance.

Ashok

TOPICS
Scripting

Views

3.1K

Translate

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 1 Correct answer

Enthusiast , Aug 17, 2017 Aug 17, 2017

This function can apply to all textFrames.

function reduceSizeOfOversetText(t) {

    var d;

    while (

        d = t.duplicate(),

        d.name = 'temp',

        d.convertAreaObjectToPointObject(),

        d = t.parent.textFrames['temp'],

        d.contents.replace(/[\x03\r]/g, '') !== t.contents.replace(/[\x03\r]/g, '')

    ) {

        d.remove();

        t.textRange.characterAttributes.size -= 1;

    }

    d.remove();

}

reduceSizeOfOversetText(app.selection[0]);

Another way: you can create an temp textFrame

...

Votes

Translate

Translate
Adobe
Valorous Hero ,
Aug 07, 2017 Aug 07, 2017

Copy link to clipboard

Copied

Let's make a text overset function which detects the overset on multiline text.

There may be several ways to accomplish this, I am thinking of the following:

  1. Read the text of the entire text area and take note of it in a variable.
  2. Go through every line of the text area and replace the text in the variable previously defined by taking the line's text and removing it from the text contents.
  3. If all the lines were showing without overset, the result of the contents variable should be an empty string.
  4. If there was overset, then some of the contents did not get replaced by some of the lines which were hidden below the frame.

Here is a snippet to start you off:

#target illustrator

function test(){

  var doc = app.activeDocument;

  var sel = doc.selection[0];

  var contents = sel.contents;

 

  for(var i = 0; i < sel.lines.length; i++){

    contents = contents.replace(sel.lines.contents, "");

  }

  alert(contents);

};

test();

Votes

Translate

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 ,
Aug 08, 2017 Aug 08, 2017

Copy link to clipboard

Copied

Thank you for your reply. the above code is for selected text frame(frame need to selected manually) but my request is in an illustrator document where there are more than 100 textframes and i need to identify the overset text and reduce the fontsize of the overset text in all the 100 text frame.

Kindly help me on this please.

Regards

Ashok

Votes

Translate

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 ,
Aug 17, 2017 Aug 17, 2017

Copy link to clipboard

Copied

Hi Silly-V

As per your guidance selected text frame overset text is showing correctly. but i want to identify the overset text and reduce the font to fix the overset text.below code shows only the entire line like screenshot & code attached below.

Capture.JPG

function texts(){

if(documents.length > 0)

{

   var docRef = activeDocument;

  

   // clear the old selection

   var piArr=[];

   activeDocument.selection = null;

  //var contents = sel.contents;

   // select all text art items that contain the target string

   for (j = 0; j < docRef.textFrames.length; j++){

  // var t=docRef.textFrames

//var t=docRef.textFrames

var t=docRef.textFrames

contents=t.contents

 

   

        for(var i = 0; i < t.lines.length; i++){

          contents = contents.replace(t.lines.contents, "");

//piArr.push(contents);

alert(contents)

piArr.push(contents);

   //   alert(t.textRange.size);

       if(contents!=""){

       

        var fs=t.textRange.size

         if (fs<=6)

    {

alert(fs+"small font"+"Still overset") 

//exit();

}

else

{

   

   t.textRange.characterAttributes.size = fs-1

   s()

  }

   

}

else

{

}

//var t=docRef.textFrames

//contentString = docRef.textFrames.contents;//  contentString.lines.length;

// var contents = docRef.textFrames.contents;

  //contentString.length;

}

}}

function s(){

if(piArr=="")

{

    exit()

    }else{

  

   alert("still overset")

texts()

}}

}

   //redraw();

//}

//}

texts()

Regards

Ashok

Votes

Translate

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
Enthusiast ,
Aug 17, 2017 Aug 17, 2017

Copy link to clipboard

Copied

This function can apply to all textFrames.

function reduceSizeOfOversetText(t) {

    var d;

    while (

        d = t.duplicate(),

        d.name = 'temp',

        d.convertAreaObjectToPointObject(),

        d = t.parent.textFrames['temp'],

        d.contents.replace(/[\x03\r]/g, '') !== t.contents.replace(/[\x03\r]/g, '')

    ) {

        d.remove();

        t.textRange.characterAttributes.size -= 1;

    }

    d.remove();

}

reduceSizeOfOversetText(app.selection[0]);

Another way: you can create an temp textFrame and set .nextFrame property to it, then compare TextFrame's contents to Story's contents.

Votes

Translate

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
Valorous Hero ,
Aug 17, 2017 Aug 17, 2017

Copy link to clipboard

Copied

Hey, nice job! what is the character "\x03" ?

Votes

Translate

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
Enthusiast ,
Aug 17, 2017 Aug 17, 2017

Copy link to clipboard

Copied

So called "soft return"? Use centents.toSource() to get it.

When convert area text to point text, line end will add a soft return, and if orginal line end is soft return, it will convert to hard return.

Votes

Translate

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 ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

Thank you so much for your help.

But the script only run on selected text frame.

i want to identify the overset text and reduce the font size for all overset text frames.

#target illustrator

function test(){

var doc = app.activeDocument;

var sel = doc.selection[0];

var contents = sel.contents;

for(var i = 0; i < sel.lines.length; i++){

contents = contents.replace(sel.lines.contents, "");

}

alert(contents);

    reduceSizeOfOversetText(app.selection[0]); 

};

test();

function reduceSizeOfOversetText(t) { 

        var d; 

        while ( 

            d = t.duplicate(), 

            d.name = 'temp', 

            d.convertAreaObjectToPointObject(), 

            d = t.parent.textFrames['temp'], 

            d.contents.replace(/[\x03\r]/g, '') !== t.contents.replace(/[\x03\r]/g, '') 

        ) { 

            d.remove(); 

            t.textRange.characterAttributes.size -= 1; 

        d.remove(); 

    } 

     

     

kindly help on this.

Regards

Ashok

Votes

Translate

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 Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

LATEST

Hi all, for anyone finding this old thread, also please have a look at @femkeblanco's answer here. - Mark

Votes

Translate

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