Skip to main content
Bedazzled532
Inspiring
April 13, 2023
Answered

Script to split and merge textboxes in InDesign

  • April 13, 2023
  • 1 reply
  • 2106 views

Hello all. I have multiple text boxes threaded (screeshot attached). When the text in frame is too much to handle, I select 2/3 frames and then merge them. This part is easy using pathfinder. BUT issue is splitting them.
I just want to select the text frame and just run the script to split text frame to exact halves or into 3 parts. How do I achieve this using script ? Or is there a better method?
Currently I am creating a new text box and then re-threading them to achieve what I want but its too cumbersome.
Any help would be appreciated. Thanks.

This topic has been closed for replies.
Correct answer m1b

Just to warm up my brain a little, here's a version of Peter's script converted into a function:

function main() {
    var frames = splitFrame(app.selection[0], prompt('Split into how many frames?', '2'));
}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Split Frame');


/**
 * Splits a text frame by dividing the width.
 * @9397041 {TextFrame} frame - an Indesign TextFrame.
 * @9397041 {Number} frameCount - the desired number of frames.
 * @Returns {Array<TextFrame>} - the split frames.
 */
function splitFrame(frame, frameCount) {

    frameCount = Number(frameCount);

    if (
        isNaN(frameCount) || frameCount < 2
        || frame == undefined || !frame.isValid
    )
        return;

    var gb = frame.geometricBounds,
        w = (gb[3] - gb[1]) / frameCount,
        tf = frame,
        frames = [tf];

    tf.geometricBounds = [gb[0], gb[1], gb[2], gb[1] + w];

    for (var i = 1; i < frameCount; i++) {
        frames.push(
            tf = frame.parentPage.textFrames.add({
                geometricBounds: [gb[0], gb[1] + (w * i), gb[2], gb[1] + (w * (i + 1))],
                previousTextFrame: tf
            })
        );
    }

    return frames;

};

- Mark 

1 reply

Braniac
April 13, 2023

Use the frame's geometric bounds. To split the selected frame into three parts:

frame = app.selection[0];
gb = frame.geometricBounds;
w = (gb[3]-gb[1])/3;

// Resize the original frame

frame.geometricBounds = [gb[0], gb[1], gb[2], gb[1]+w];

// Then add two frames:

frame.parentPage.textFrames.add ({
  geometricBounds: [gb[0], gb[1]+w, gb[2], gb[1]+(w*2)],
});

frame.parentPage.textFrames.add ({
  geometricBounds: [gb[0], gb[1]+(w*2), gb[2], gb[3]],
});
m1b
m1bCorrect answer
Braniac
April 13, 2023

Just to warm up my brain a little, here's a version of Peter's script converted into a function:

function main() {
    var frames = splitFrame(app.selection[0], prompt('Split into how many frames?', '2'));
}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Split Frame');


/**
 * Splits a text frame by dividing the width.
 * @9397041 {TextFrame} frame - an Indesign TextFrame.
 * @9397041 {Number} frameCount - the desired number of frames.
 * @Returns {Array<TextFrame>} - the split frames.
 */
function splitFrame(frame, frameCount) {

    frameCount = Number(frameCount);

    if (
        isNaN(frameCount) || frameCount < 2
        || frame == undefined || !frame.isValid
    )
        return;

    var gb = frame.geometricBounds,
        w = (gb[3] - gb[1]) / frameCount,
        tf = frame,
        frames = [tf];

    tf.geometricBounds = [gb[0], gb[1], gb[2], gb[1] + w];

    for (var i = 1; i < frameCount; i++) {
        frames.push(
            tf = frame.parentPage.textFrames.add({
                geometricBounds: [gb[0], gb[1] + (w * i), gb[2], gb[1] + (w * (i + 1))],
                previousTextFrame: tf
            })
        );
    }

    return frames;

};

- Mark 

Bedazzled532
Inspiring
April 14, 2023

Awesome!!! Loved it. Thanks so much.

I dont know what I would have done without great people like you! @Peter Kahrel @m1b