Thursday, September 01, 2005

 

Script of the Day -- Fit Frame to Content

It comes to me in a blinding flash that I can enhance the behavior of Fit Frame to Content when the selection is an insertion point or text. I believe that without exception, when I issue that command in that state, I want to end up with the pointer tool active and the frame selected. So, let's give it a go and see:
//DESCRIPTION: Alt form of Fit Frame to Content

Object.prototype.isPureText = function() {
 switch(this.constructor.name){
  case "InsertionPoint":
  case "Character":
  case "Word":
  case "TextStyleRange":
  case "Line":
  case "Paragraph":
  case "TextColumn":
  case "Text":
   return true;
  default :
   return false;
 }
}

// +++++++ Main Script Starts Here ++++++++++++++++++++++++++

if ((app.documents.length != 0) && (app.selection.length != 0)) {
 if (app.selection[0].isPureText()) {
  app.select(app.selection[0].parentTextFrames[0]);
 }
 app.selection[0].fit(FitOptions.frameToContent);
} 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.
}
Of the 38 lines in this script, all but four came straight from my snippet library. The critical four lines are:
 if (app.selection[0].isPureText()) {
  app.select(app.selection[0].parentTextFrames[0]);
 }
 app.selection[0].fit(FitOptions.frameToContent);
This uses the isPureText() method to see if the selection is text (by "pure" text I mean "not a text frame" -- I have another method named isText() that I use if I want to allow text frames to be included in whatever the script is about to do).

In this case, if the selection is text, then the script simply selects its parent text frame (note the syntax I've used is CS2-only syntax; for CS, I'd have used simply .parentTextFrame; although that would not always work if the text spanned more than one text frame. You could argue that my CS2 code here also doesn't work if the text spans more than one frame, but that just doesn't happen in practice when I use this feature) and applies the fit command to it. This leaves the frame selected when it completes, which is exactly what I wanted.

On the other hand, if the selection is not text, then the command behaves just as it always did.

The next step is to save the script somewhere sensible -- I put it in my Text scripts subfolder and named it FitFrameToContent.jsx -- and then assign to it the Command-Option-C shortcut which was previously assigned to the menu item Fit Frame to Content on the Fitting submenu of the Object menu and away we go.

Serendipity

The script doesn't quite behave as I expected. It leaves the text tool active. But this means that I just hold down the Option key to move the text frame where I want it (nine times out of ten, when I need this feature for text frames, I'm constructing a caption for an image).

Comments:
What about if I want content to fit frame - FitOptions.CONTENT_TO_FRAME?
 
Post a Comment

<< Home

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