Saturday, January 14, 2006

 

Zoom in on Object

Having set myself the goal, I just couldn't resist tackling this job tonight (particularly as our local PBS station has pre-empted its British comedies to do fund-raising this evening). And it's quite straightforward.

This is an unusual function in that it doesn't return to the caller. It either throws an error if you try to zoom in on an object that doesn't have a geometric bounds property (this will also happen if a story editor window is at the front) or it exits back to the user on the theory that there's not much point in zooming in on something if you don't stop to let the user see the result.

The script takes advantage of the zoom feature that fits a page into a window. Because we can get the bounds of the page and the zoom percentage of the window, we know what percentage corresponds to the size of a page, so all we have to do is calculate the two ratios of page height to object height and page width to object width and then multiply the page-fitting percentage by the smaller of those two ratios:
function zoomObject(theObj) {
 try {
  var objBounds = theObj.geometricBounds;
 } catch (e) {
  throw "Object doesn't have bounds."
 }
 var ObjHeight = objBounds[2] - objBounds[0];
 var ObjWidth = objBounds[3] - objBounds[1];
 var myWindow = app.activeWindow;
 var pageBounds = myWindow.activePage.bounds;
 var PgeHeight = pageBounds[2] - pageBounds[0];
 var PgeWidth = pageBounds[3] - pageBounds[1];
 var hRatio = PgeHeight/ObjHeight;
 var wRatio = PgeWidth/ObjWidth;
 var zoomRatio = Math.min(hRatio, wRatio);
 app.select(theObj); // to make active the page that holds theObj
 myWindow.zoom(ZoomOptions.fitPage);
 myWindow.zoomPercentage = myWindow.zoomPercentage * zoomRatio;
 exit() // Because there's no point in doing this if you don't exit to let the user see
}
I'm thinking that for text, it ought to be possible to come up with a set of bounds, so perhaps this could be extended to more kinds of object, but for the moment, I shall satisfy myself with this version.

Update: This version doesn't seem to be able to turn to the page of an inline or anchored object.

Comments:
Nice script, but what is "throw" (throw "Object doesn't have bounds.")?


This gives an error.

Martin
 
Hi Martin,

There have been so few comments here that I tend to forget to look for them.

Throw does indeed cause an error. That's what it does. It throws an error message consisting of the text provided.

Dave
 
Post a Comment

<< Home

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