Tuesday, August 30, 2005

 

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, it's a little more complicated to do this kind of thing in InDesign.

On the other hand, I have grown to prefer InDesign's 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 InDesign's 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 I'll 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 you'll 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 story

if (app.selection[0].constructor.name == "TextFrame"){
Notice that I don't bother to check if there's actually a selection. Perhaps, given the popularity of this script and how widely spread it has become, I should add that check.
 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();
 myFrame.remove();
}
Well, what do you know! This little script that I've used so many times has a bug! What if the frame is empty? OK, here's a new and improved version that both checks to make sure there's a selection in an opened document and that deals with the possibility that the selected frame is empty:
//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.
}

Comments:
Thank you!!!

When I started using the SplitStory that came with IndesginCS2 I hoped that it only splited the text frames I had selected... imagine how disappointed I was when I found every single frame was splited >_< But since I could work around that I kept using... but now with your script I have to worry no more ^_~

Thank you very much! It´s a great script!
 
Hi,

It is nice and i need a help from you, can you give me an idea, to pull images and its caption from a single folder into the indesign file citations.
 
I recommend you post that kind of issue in the Adobe User to User InDesign Scripting forum.

http://www.adobeforums.com/cgi-bin/webx?14@@.eea52bc

Dave
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?