Friday, September 02, 2005

 

Very Hectic Day

Today was very hectic. I wrote one special-purpose script, so let me share it here. I had created a map in the form of a group of elements in one InDesign document which was created from scratch for the purpose. Consequently, it used my application default setting for TextFramePreferences/Ignore Text Wrap, which is off. In the series of documents I'm working on, this option is set the other way, and so I overlooked the issue until I tried to use the map in my live document as part of a feature box that intrudes into the main story and so has text wrap turned on.

All the text frames in my map (country names, mainly) instantly became overset. Bah humbug!

So, I decided to write a quick script to solve the problem. Here's what I ended up with:
//DESCRIPTION: Set all text frames in selected group to Ignore Text Wrap

if ((app.documents.length != 0) && (app.selection.length != 0)) {
 var myFrames = app.selection[0].textFrames;
 var myLim = myFrames.length;
 for (j = 0; myLim > j; j++) {
  myFrames[j].textFramePreferences.ignoreWrap = true;
 }
} 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.
}
If you've been following along, you'll recognize the framework here, so the guts of the script are just these lines:
 var myFrames = app.selection[0].textFrames;
 var myLim = myFrames.length;
 for (j = 0; myLim > j; j++) {
  myFrames[j].textFramePreferences.ignoreWrap = true;
 }
Like I said, a special purpose script. Consequently, it does next to no error checking, and simply assumes that the selection is a group (having established thanks to my framework that there is a selection). What's more, it makes the assumption that all the text frames are at the first level within the group (which I happened to know was true).

I confess that I was disappointed that I couldn't use the everyItem() construction here. Indeed, my first attempt at the script had just one line:
app.selection[0].textFrames.everyItem().textFramePreferences.ignoreWrap = true;
but this doesn't work, and I'm not entirely sure why not. Fodder for more research!

By the way, if you're an experienced JavaScripter, you might think my for statements a tad odd: why do I use "j" and not "i" like everybody else, and what's with the way I write the loop test?

Well, it happens that in the font I use for my scripting at the size I use it (Verdana at 10 points), the "i" is a tad hard to distinguish from the "l" so I just go with j, then k, then m, then n (if I ever get that deep).

And, as for the test, tradition would have me write j < myLim but if I do that, when it comes time to post the script on a forum (or even here), I'd have to edit all those < characters and replace them with &lt; which, bluntly, is more trouble than it's worth. So, I write my comparisons using ">" which doesn't seem to upset browsers (although some purists have argued rather heatedly with me about this). To me, myLim > j has become the natural construction.

Comments: Post a Comment

<< Home

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