Wednesday, July 4, 2018
Script of the Day Break Out Text Frame
Script of the Day Break Out Text Frame
One of the things that PageMaker users yearn for is the ease of splitting stories into their frames or into two (or more) stories. With InDesign treating the story as a separate entity from the frames that contain it, its a little more complicated to do this kind of thing in InDesign.
On the other hand, I have grown to prefer InDesigns approach because when working interactively with a story in a document, it is great to be able to do such things as add a text frame on the pasteboard to a story so I can have access to the next text (without turning the page) as I work on a right-hand page. Life is full of these kinds of swings-and-roundabouts options and generally speaking, I have ended up preferring InDesigns approach to things -- although there are a small number of exceptions: perhaps I should list them one of these days.
This particular issue (splitting a story) lends itself to some fairly easy scripting solutions. By the way, one of the sample scripts that ships with InDesign CS2 is actually called SplitStory.jsx, but it does something different from my SplitStory.jsx (which Ill write about tomorrow). The bundled script breaks a story into as many stories as there are text frames in the story.
First, a script to break out an individual text frame (For those not familiar with PageMaker, if you selected a frame (or text block) in PageMaker, cut it and then pasted back in place, the effect would be the same as what this script does. Try the same thing in InDesign, and youll end up with the text in two places: in the new frame and also at the start of the next frame of the original story.):
//DESCRIPTION: Breaks out selected text frame from a storyNotice that I dont bother to check if theres actually a selection. Perhaps, given the popularity of this script and how widely spread it has become, I should add that check.
if (app.selection[0].constructor.name == "TextFrame"){
myFrame = app.selection[0];Since we know a text frame is selected, we create a reference to it in the variable named myFrame.
myText = myFrame.texts[0];Because duplicating a frame also duplicates its contents, we need this reference to the text in the original frame so that we can delete it later.
myDupeFrame = myFrame.duplicate();This creates an exact copy of myFrame. I wonder why I bothered to create a reference to it (myDupeFrame) given that I never use it. However, this new frame is exactly what we wanted to extract from the original story. So now all we have to do is clean up the original story by deleting the text in the frame and then deleting the frame:
myText.remove();Well, what do you know! This little script that Ive used so many times has a bug! What if the frame is empty? OK, heres a new and improved version that both checks to make sure theres a selection in an opened document and that deals with the possibility that the selected frame is empty:
myFrame.remove();
}
//DESCRIPTION: Breaks out selected text frame from a story
if ((app.documents.length != 0) && (app.selection.length != 0)) {
if (app.selection[0].constructor.name == "TextFrame"){
myFrame = app.selection[0];
myText = myFrame.texts[0];
myDupeFrame = myFrame.duplicate();
try{
myText.remove();
} catch (e) {} // An error indicates the frame was already empty
myFrame.remove();
}
} else {
errorExit();
}
// +++++++ Functions Start Here +++++++++++++++++++++++
function errorExit(message) {
if (arguments.length > 0) {
if (app.version != 3) { beep() } // CS2 includes beep() function.
alert(message);
}
exit(); // CS exits with a beep; CS2 exits silently.
}