Skip to content

Commit

Permalink
take into account text frames
Browse files Browse the repository at this point in the history
  • Loading branch information
pchiorean committed Apr 8, 2020
1 parent 42a9df1 commit f6f97be
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions zoom2selection.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Zoom to selection v1.3.1
Zoom to selection v1.4.0
© April 2020, Paul Chiorean
This script zooms to the selected objects or, if nothing is selected, to the current page.
*/
Expand All @@ -8,31 +8,41 @@ var doc = app.activeDocument;
var window = app.activeWindow;
var selPage = window.activePage;
var selObj = doc.selection;
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

// Get target dimensions
if (selObj.length != 0) {
var selObj_y1 = selObj[0].visibleBounds[0];
var selObj_x1 = selObj[0].visibleBounds[1];
var selObj_y2 = selObj[0].visibleBounds[2];
var selObj_x2 = selObj[0].visibleBounds[3];
for (i = 1; i < selObj.length; i++) {
selObj_y1 = Math.min(selObj[i].visibleBounds[0], selObj_y1);
selObj_x1 = Math.min(selObj[i].visibleBounds[1], selObj_x1);
selObj_y2 = Math.max(selObj[i].visibleBounds[2], selObj_y2);
selObj_x2 = Math.max(selObj[i].visibleBounds[3], selObj_x2);
if (selObj[0].hasOwnProperty("parentTextFrames")) {
// We are inside a text frame, get frame bounds
var selObj_y1 = selObj[0].parentTextFrames[0].visibleBounds[0];
var selObj_x1 = selObj[0].parentTextFrames[0].visibleBounds[1];
var selObj_y2 = selObj[0].parentTextFrames[0].visibleBounds[2];
var selObj_x2 = selObj[0].parentTextFrames[0].visibleBounds[3];
var sel = doc.selection; // Save selection
app.select(selObj[0].parentTextFrames[0]); // Select frame
} else {
var selObj_y1 = selObj[0].visibleBounds[0];
var selObj_x1 = selObj[0].visibleBounds[1];
var selObj_y2 = selObj[0].visibleBounds[2];
var selObj_x2 = selObj[0].visibleBounds[3];
for (i = 1; i < selObj.length; i++) { // Find extremities
selObj_y1 = Math.min(selObj[i].visibleBounds[0], selObj_y1);
selObj_x1 = Math.min(selObj[i].visibleBounds[1], selObj_x1);
selObj_y2 = Math.max(selObj[i].visibleBounds[2], selObj_y2);
selObj_x2 = Math.max(selObj[i].visibleBounds[3], selObj_x2);
}
}
var W_obj = selObj_x2 - selObj_x1;
var H_obj = selObj_y2 - selObj_y1;
} else {
} else { // Nothing is selected, zoom to page
var W_obj = selPage.bounds[3] - selPage.bounds[1];
var H_obj = selPage.bounds[2] - selPage.bounds[0];
}
// Get window dimensions
var W_win = window.bounds[3] - window.bounds[1];
var H_win = (window.bounds[2] - window.bounds[0]) * 1.33;

var zoom = H_win / H_obj;
if (W_obj * zoom > W_win) { zoom = W_win / W_obj }
zoom = zoom * 10 * 1.5;

if (zoom > 4000) { zoom = 4000 };
window.zoom(ZoomOptions.FIT_PAGE);
window.zoomPercentage = zoom;
var zoom = (W_obj * H_win / H_obj > W_win) ? W_win / W_obj : H_win / H_obj;
zoom = Math.min(zoom * 10 * 1.5, 4000);// 4000% is max zoom
window.zoom(ZoomOptions.FIT_PAGE); try { window.zoomPercentage = zoom } catch (e) {}; // OoB err?!
try { app.select(sel) } catch (e) {}; // Restore initial selection

0 comments on commit f6f97be

Please sign in to comment.