Skip to main content
SuzzyFlamingo
Inspiring
September 3, 2025
Question

Help to loop script

  • September 3, 2025
  • 1 reply
  • 224 views

I feel quite guilty about asking for help with this.  I know that the noble members of this form are not here to do work for me.  But I'm really stuck and ChatGPT just can't cut it.  In my line of expertise (life coaching)  I give a lot of my time away for others. I would be happy to do so for anybody here who needs assistance 🙂

The following script works fine on one selected text box. What I would like to do is alter that it should loop through **the entire document** and execute the same for any text box that has a text overflow. The insertion text in the new text box should not be altered. If someone could assist, I would be most appreciative.

Thank you, and have a good day!
Susan Flamingo

PS If somebody could create a third version that would do the same for the frames of the **current story** I would be ecstatic 🙂

 

so:

1. current version

2. Every overflowing text frame in the entire doc

3. Every overflowing text frame in the story of the selected text frame

 

------------------------>

// InDesign ExtendScript (JSX)
// Usage: when text overflows in a text frame, a new text frame is created under the first frame with the first word of the second frame
 
(function () {
    if (app.documents.length === 0) { alert("Open a document."); return; }
    if (app.selection.length !== 1) { alert("Select one text frame (or text inside it)."); return; }
 
    var sel = app.selection[0];
    var frame1;
 
    // Resolve selection to a TextFrame
    if (sel instanceof TextFrame) {
        frame1 = sel;
    } else if (sel.parentTextFrames && sel.parentTextFrames.length > 0) {
        frame1 = sel.parentTextFrames[0];
    } else {
        alert("Selection is not in a text frame.");
        return;
    }
 
    // Get the next frame in the thread
    var frame2 = frame1.nextTextFrame;
    if (!frame2 || !frame2.isValid) {
        alert("Selected frame is not linked to a next frame.");
        return;
    }
 
    if (frame2.words.length === 0) {
        alert("The next frame has no text.");
        return;
    }
 
    frame2.parentPage.name
//alert("The overflow continues on page: " + frame2.parentPage.name)
var firstWord = frame2.words[0].contents;
alert (firstWord)
var firstWord = "[ההמשך נמצא בעמ\'" + " " + frame2.parentPage.name + " " + "ד\"ה" + " " + firstWord + "]"
//---"[ההמשך נמצא בעמ\' 222 ד\"ה" + " " + firstWord + "]"
    // Place a new frame right under frame1 (same width)
    var gb = frame1.geometricBounds; // [y1, x1, y2, x2]
    var gap = 6;                     // space below frame1, in pts
    var top = gb[2] + gap;
    var left = gb[1];
    var right = gb[3];
    var initialHeight = 20;
    var bottom = top + initialHeight;
 
    // Add to the same parent container and layer
    var parentContainer = frame1.parent;
    var newFrame = parentContainer.textFrames.add();
    newFrame.geometricBounds = [top, left, bottom, right];
    newFrame.contents = firstWord;
    newFrame.itemLayer = frame1.itemLayer;
 
    // Apply object style "MilatHemshech2" if it exists
    try {
        var objStyle = app.activeDocument.objectStyles.itemByName("MilatHemshech2");
        if (objStyle.isValid) {
            newFrame.appliedObjectStyle = objStyle;
        } else {
            alert('Object Style "MilatHemshech2" not found. Frame created without style.');
        }
    } catch (e) {
        alert("Error while applying object style: " + e);
    }
 
    // Allow the frame to auto-size vertically to fit the word
    try {
        newFrame.textFramePreferences.autoSizingReferencePoint = AutoSizingReferenceEnum.TOP_LEFT_POINT;
        newFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY;
    } catch (e) {
        // ignore if not supported
    }
 
})();

<-------------------------------------

1 reply

Community Expert
September 4, 2025

Hi @SuzzyFlamingo,

This script does something more that resolving ovreset. It wants a selection to a textframe that is threaded. Then it takes the first word of the second frame in the thread. Appends this word with some static text and the frames page name. Then it creates a new textframe and puts the the content mentioned above in the newly created frame. It also sets the autosizing options of this new frame to resolve oversets.

Do you want all this to be done as per you changed needs or just want a script that resolves overset by setting the autosizing options of any frame that has an overset?

-Manan

-Manan
SuzzyFlamingo
Inspiring
September 4, 2025

Yes, everything on every frame with overflow for entire story/doc.

Thank you

Sysan

Community Expert
September 4, 2025

Try the following. You can run this after your previous script

for (var i = 0; i < app.documents[0].textFrames.length; i++){
    var tf = app.documents[0].textFrames[i]
    if (tf.overflows) {
        tf.textFramePreferences.autoSizingReferencePoint = AutoSizingReferenceEnum.TOP_LEFT_POINT;
        tf.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY;
    }
}

This will try to resolve the overset in any frame on the active document by setting the autogrow to grow vertically

-Manan

-Manan