Skip to main content
Known Participant
August 18, 2023
Question

Single Multi Column text frame to individual separate text frames - Nearly working - Please help

  • August 18, 2023
  • 1 reply
  • 174 views

Hello All, 

I have the bulk of a script which takes a single frame of which has multi columns to split each of the columns into spearate, but threaded boxes retaining the same gutter etc

the only issue that the script repeats the text an eqaual amount of times as there are columns 

as the script duplicates the single frame the same amount of times as there are columns and also then once done drops the text frame to only have one column.

Below is the code

Also this can be undone in one go to which I saw another online which was much longer code and couldnt be undone in one go

Any help or advice would be great

Best 

Smyth


// Check if an InDesign document is open
if (app.documents.length > 0) {
    var doc = app.activeDocument;

    // Check if a text frame is selected
    if (doc.selection.length === 1 && doc.selection[0] instanceof TextFrame) {
        var selectedTextFrame = doc.selection[0];

        app.doScript(function () {
            // Get the linked story of the text frame
            var story = selectedTextFrame.parentStory;

            // Check if the text frame has multiple columns
            if (selectedTextFrame.textColumns.length > 1) {
                var columns = selectedTextFrame.textColumns;
                var gutter = selectedTextFrame.textFramePreferences.textColumnGutter;
                var columnWidth = (selectedTextFrame.geometricBounds[3] - selectedTextFrame.geometricBounds[1] - (gutter * (columns.length - 1))) / columns.length;

                // Create an array to store the duplicated frames
                var duplicatedFrames = [];

                for (var i = 0; i < columns.length; i++) {
                    var newX1 = selectedTextFrame.geometricBounds[1] + (columnWidth + gutter) * i;
                    var newX2 = newX1 + columnWidth;

                    // Duplicate the selectedTextFrame
                    var newFrame = selectedTextFrame.duplicate();
                    newFrame.geometricBounds = [selectedTextFrame.geometricBounds[0], newX1, selectedTextFrame.geometricBounds[2], newX2];

                    // Set column count to 1 for the duplicated frame
                    newFrame.textFramePreferences.textColumnCount = 1;

                    // Add the duplicated frame to the array
                    duplicatedFrames.push(newFrame);
                }

                // Remove the original multi-column text frame
                selectedTextFrame.remove();

                // Thread the duplicated frames
                for (var i = 0; i < duplicatedFrames.length - 1; i++) {
                    duplicatedFrames[i].nextTextFrame = duplicatedFrames[i + 1];
                }

                // Display a message
                alert("Columns split into individual text frames, threaded, and set to single column.");
            } else {
                alert("Selected text frame does not have multiple columns.");
            }
        }, undefined, undefined, UndoModes.ENTIRE_SCRIPT);
    } else {
        alert("Please select a single text frame.");
    }
} else {
    alert("No open documents.");
}




This topic has been closed for replies.

1 reply

Robert at ID-Tasker
Legend
August 18, 2023

Replying from my phone, and I'm not JS guy, so... 

 

What I would do:

  1.  Duplicate TF, convert to single, resize etc. - so what you are doing for each TextColumn, 
  2.  Clear the duplicated TF - newFrame.contents = "" - you can't just duplicate and leave the old contents, 
  3.  Instead of pushing into an array and then looping through the array - link the first one to the source and then new one to the last one - code will be shorter and quicker, 
  4.  At the end - delete source TF - as new ones are already linked with it - text will flow to them automatically. 

 

BUT - why do you want to do that? Just for fun or is there a practical reason? 

 

Known Participant
August 18, 2023

I just really need a script that can do this, there is one online by Peter forget the name but its not just a double click and work. 

 

Let me see what you said and ill see if it helps 

 

thank you