Skip to main content
Known Participant
February 19, 2023
Answered

alert if the document contains overset text warning

  • February 19, 2023
  • 2 replies
  • 9639 views

var doc = app.activeDocument;
var textFrames = doc.textFrames;
var numTextFrames = textFrames.length; for (var i = 0; i < numTextFrames; i++) { var tf = textFrames[i]; if (tf.kind == TextType.AREATEXT && tf.textPath.contents.length > 0 && tf.textPath.overflows) { alert("Overset text detected in text frame " + (i+1)); } }

can anyone here help me on this!

This returns error

or any other way to alert

 

This topic has been closed for replies.
Correct answer femkeblanco

Here with the files

https://we.tl/t-aGwruwK7if



Try this

var doc = app.activeDocument;
var textFrames = doc.textFrames;
var numTextFrames = textFrames.length;
for (var i = 0; i < numTextFrames; i++) {
    var tf = textFrames[i];
    if (tf.kind == TextType.AREATEXT &&
        tf.contents.length > 0 &&
        overflows(tf)) {
            alert("Overset text detected in text frame " + (i + 1));
            // tf.selected = true;
    }
}

function overflows(tf) {
    var lastLine = tf.lines[tf.lines.length - 1];
    var textRange1 = lastLine.contents;
    var textRange2 = "";
    var i = tf.characters.length - 1;
    var difference = (tf.characters.length - lastLine.contents.length) - 1;
    var lastChar = tf.characters[tf.characters.length - 1].contents;
    for (var j = 0; j < 32; j++) {
        if (lastChar == String.fromCharCode(j) && lastChar != "\t") {
            i--, difference--;
            break;
        }
    }
    for (i; i > difference; i--) {
        textRange2 = tf.characters[i].contents + textRange2;
    }
    return textRange1 != textRange2;
}

2 replies

femkeblanco
Legend
February 19, 2023

An alternative function (not rigorously tested).

var doc = app.activeDocument;
var textFrames = doc.textFrames;
var numTextFrames = textFrames.length;
for (var i = 0; i < numTextFrames; i++) {
    var tf = textFrames[i];
    if (tf.kind == TextType.AREATEXT &&
        tf.contents.length > 0 &&
        overflows(tf)) {
            alert("Overset text detected in text frame " + (i + 1));
    }
}

function overflows(tf) {
    var lastLine = tf.lines[tf.lines.length - 1];
    var textRange1 = lastLine.contents;
    var textRange2 = "";
    for (var i = tf.characters.length - 1;
        i > (tf.characters.length - lastLine.contents.length) - 1;
        i--) {
        textRange2 = tf.characters[i].contents + textRange2;
    }
    return textRange1 != textRange2;
}
Known Participant
February 20, 2023

@m1b @femkeblanco 

Thankyou both

I am trying and testing both

@m1b 

One thing with the first method is the layers should be unlocked, also the sublayers, sub groups.
sometimes with multiple elements, it returns an error, but I am analysing it.

@femkeblanco 

With your method as @m1b said is clear but i tried in multiple documents with multiple elements, it also alerts for multiple frames which does n't have any overset warning.

Known Participant
February 23, 2023

Hi @femkeblanco, I tried your updated version and it worked perfectly on the sample file. As far as I can tell, you solved the problem, and when I tried I don't think I really even understood the issue of the extra linefeed correctly, so thank you for showing the way!

 

@thirukumarans77083 You may replace my version with this one if you like. Personally I prefer @femkeblanco's version because it does no DOM manipulation (mine makes a temp layer and duplicate textframe), but in practice they are probably indistinguishable to the user. Bear in mind that you will need to deal with the locked or hidden layers/items problem by setting those to false (see example in my script above).

- Mark


@m1b @femkeblanco Thank you very much, I have tested it working well on the documents shared, but some of the tested files still show the display alert of the textbox which doesn't have the overset warning.

m1b
Community Expert
Community Expert
February 19, 2023

Hi @thirukumarans77083, I've put together a function to do this, which I found in another area on this forum. See code for link. - Mark

var doc = app.activeDocument;
var textFrames = doc.textFrames;
var numTextFrames = textFrames.length;
for (var i = 0; i < numTextFrames; i++) {
    var tf = textFrames[i];
    if (textFrameIsOverset(tf)) {
        alert("Overset text detected in text frame " + (i + 1) + ' ' + tf.name);
    }
}


/**
 * Returns true if an Illustrator Area Text Frame is overset.
 * @author based on code by moluapple
 * @url https://community.adobe.com/t5/illustrator-discussions/illustrator-javascript-for-quot-identify-overset-text-in-a-text-frame-and-reduce-the-font-size-quot/td-p/9273739
 * @param {TextFrame} tf - an Illustrator text frame.
 * @returns {Boolean}
 */
function textFrameIsOverset(tf) {
    var isOverset = !tf.hasOwnProperty('kind') || tf.kind != TextType.AREATEXT;
    var dup = tf.duplicate();
    dup.name = 'temp';
    dup.convertAreaObjectToPointObject();
    dup = tf.parent.textFrames['temp'];
    isOverset = dup.contents.replace(/[\x03\r]/g, '') !== tf.contents.replace(/[\x03\r]/g, '')
    dup.remove();
    return isOverset;
};