Script Description
This script will convert textframe columns to individual frames in inDesign. The purpose here is if you create a textframe using columns and then later decide you would like to adjust the heights or widths of the columns separately.
Looking at the Script
To start, the script checks what the user has selected. If they have selected a textframe the script will run, otherwise it will alert them on how to use this script. Then the script gets a set of properties it will need to use later. It checks if the text frame is part of a thread with a either a frame before or after it. It count the amount of columns in the frame and finds the width the gutter is set to.
if (app.selection.length != 1 || app.selection[0] != "[object TextFrame]")
{
alert("To run this script please select a textframe first.");
}
else
{
//Get properties of textframe
var myDocument = app.activeDocument;
var myTextFrame = app.selection[0];
var myTextFramePrev = myTextFrame.previousTextFrame;
var myTextFrameNext = myTextFrame.nextTextFrame;
var myColAmount = myTextFrame.textFramePreferences.textColumnCount;
var myMultipleCols = myColAmount - 1; //The cols. after the first one
var myTextFrameBounds = myTextFrame.geometricBounds;
var myGutterWidth = myTextFrame.textFramePreferences.textColumnGutter;
var myColWidth = ( (myTextFrameBounds[3] - myTextFrameBounds[1]) - (myGutterWidth * myMultipleCols) ) / myColAmount;
var myGutterAmount = myTextFrame.textFramePreferences.textColumnCount;
//Find the first textFrame in the thread that myTextFrame is in, get it's contents
myFirstTextFrame = myTextFrame;
while (myFirstTextFrame.previousTextFrame == "[object TextFrame]")
{
myFirstTextFrame = myFirstTextFrame.previousTextFrame;
}
var myContents = myFirstTextFrame.contents;
Once we have the properties of the selected textframe we can go about creating new frames for each of the columns of the original frame. I create the first new frame by itself as it's position is relative the original frame. The rest of the new frames are created in a seperate loop because their position is based on the first new frame I create. Starting with the second new frame I create, I move each frame over the space of the gutter and size it to be the same as a column from the original textframe.
// Create individual textframes if the original has more than one column
if (myColAmount > 1)
{
myTextFrame.label = "marked"; // Mark original textframe for removal
var myNewFrames = new Array();
//Create textframe for col. 1
myNewFrames[0] = myDocument.pages[0].textFrames.add();
myBounds = new Array();
myBounds[0] =myTextFrameBounds[0];
myBounds[1] = myTextFrameBounds[1];
myBounds[2] = myTextFrameBounds[2];
myBounds[3] = (myTextFrameBounds[1] +myColWidth);
myNewFrames[0].geometricBounds = myBounds;
myNewFrames[0].label = "0";
//Create textframes for col. 2 -> end
for (i = 1; i <= myMultipleCols; i ++)
{
myNewFrames[i] = myDocument.pages[0].textFrames.add();
myBounds[1] = myBounds[3] + myGutterWidth;
myBounds[3] = (myBounds[1] +myColWidth);
myNewFrames[i].geometricBounds = myBounds;
myNewFrames[i].label = (i + "") ;
}
This last part links the new frames together. It also checks to see if the original textframe was part of a threaded series of frames. If it was, the new frames I create are linked to either the frame previous to the origianls, after the original, or both. Finally the orignal textframe is deleted.
//Link the new frames
for (i =myMultipleCols; i > 0; i --)
{
j = i - 1;
myNewFrames[i].previousTextFrame = myNewFrames[j];
}
// If the texframe was part of a thread, relink the new frame to the thread
if (myTextFramePrev != null) // If there was a frame before
{
myNewFrames[0].previousTextFrame = myTextFramePrev;
}
//If there was no frame before, but a frame after
if (myTextFrameNext != null && myTextFramePrev == null)
{
myNewFrames[myMultipleCols].nextTextFrame = myTextFrameNext;
}
// if there are no frames before or after
if (myTextFramePrev == null && myTextFrameNext == null)
{
myDocument.textFrames.itemByName("marked").nextTextFrame = myNewFrames[0];
}
//Remove original textFrame
myDocument.textFrames.itemByName("marked").remove();
}
}

