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
Brainiac
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;
}
m1b
Community Expert
February 19, 2023

Hey @femkeblanco, I really like your method—much cleaner. Nice one! - Mark

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