Skip to content

Commit

Permalink
sanitize selection
Browse files Browse the repository at this point in the history
  • Loading branch information
pchiorean committed Apr 11, 2020
1 parent 9a0ab8b commit 46274bd
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions zoom2selection.jsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,58 @@
/*
Zoom to selection v1.4.2
Zoom to selection v1.4.3
© April 2020, Paul Chiorean
This script zooms to the selected objects or, if nothing is selected, to the current page.
*/

var doc = app.activeDocument;
var window = app.activeWindow;
var selPage = window.activePage;
var selObj = doc.selection;
app.scriptPreferences.measurementUnit = MeasurementUnits.PIXELS;

// Get target dimensions
if (selObj.length != 0) {
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 sel = app.selection;
var selObj = [];
// Sanitize selection
if (sel.length != 0) {
for (var i = 0; i < sel.length; i++) {
switch (sel[i].constructor.name) {
case "Rectangle":
case "Oval":
case "Polygon":
case "TextFrame":
case "Group":
case "Button":
case "GraphicLine":
selObj.push(sel[i]);
break;
}
}
var W_obj = selObj_x2 - selObj_x1;
var H_obj = selObj_y2 - selObj_y1;
} else { // Nothing is selected, zoom to page
}
if (selObj.length != 0) {
// Something is selected, get dimensions
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];
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];
// Find selection extremities
for (var 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);
}
}
var W_obj = selObj_x2 - selObj_x1;
var H_obj = selObj_y2 - selObj_y1;
} else {
// Nothing is selected, we'll zoom to page
var W_obj = selPage.bounds[3] - selPage.bounds[1];
var H_obj = selPage.bounds[2] - selPage.bounds[0];
}
Expand All @@ -48,7 +66,7 @@ var zoom = Math.min(W_win / W_obj, H_win / H_obj);
zoom = Number(zoom * 10 * 4).toFixed(1); // Adjust to taste
zoom = Math.max(5, zoom); zoom = Math.min(zoom, 4000); // Fit in 5-4000% range

// Zoom
// Zoom to target
window.zoom(ZoomOptions.FIT_PAGE);
try { window.zoomPercentage = zoom } catch (e) {
// Avoid error 30481 'Data is out of range'
Expand Down

0 comments on commit 46274bd

Please sign in to comment.