Description
This Illustrator script automatically adjusts the view to focus on the currently selected objects. It calculates the bounding box that encloses all selected items, then centers and zooms the active view so that the entire selection fits within the screen.
How It Works
- Selection Check:
- The script first verifies that there is an open document and a selection is present.
- It then determines whether the selection is an array (i.e., multiple objects).
- Calculate Bounding Box:
- The script retrieves the visible bounds of the first selected object.
- It loops through each subsequent object in the selection to determine the minimum and maximum coordinates (ul_x, ul_y for upper-left and lr_x, lr_y for lower-right corners) that define the overall bounding box.
- Centering the View:
- The center point of the bounding box is computed.
- The document view is centered on this calculated point.
- Determine Zoom Factor:
- The script retrieves the current screen bounds (width and height).
- It computes horizontal and vertical zoom factors based on how the bounding box dimensions compare to the screen size.
- Depending on the aspect ratio, it chooses the smaller zoom factor to ensure the entire selection is visible.
- The final zoom factor is slightly reduced (multiplied by 0.85) to add some padding around the selection.
- Apply Zoom and Centering:
- The active document’s view is updated with the new center point and zoom factor.
Usage
- Open or Create a Document:
Make sure you have an Illustrator document open. - Select Objects:
Select one or more objects that you want to focus on. - Run the Script:
Execute the script (e.g., via File > Scripts).
- The script will compute the overall bounding box of the selection.
- The view is then centered on that bounding box and zoomed in so that the entire selection is visible, with a small margin.
- No Selection Case:
If no objects are selected, the script resets the zoom of the active view to 1 (default zoom level).
This script is ideal for quickly focusing on a specific area of your artwork without manually panning and zooming.
JavaScript
if ( documents.length > 0)
{
if(activeDocument.selection.length >0){
mySelection = activeDocument.selection;
if (mySelection instanceof Array) {
initBounds = mySelection[0].visibleBounds;
ul_x = initBounds[0];
ul_y = initBounds[1];
lr_x = initBounds[2];
lr_y = initBounds[3];
for (i=1; i<mySelection.length; i++) {
groupBounds = mySelection[i].visibleBounds;
if (groupBounds[0]<ul_x){ul_x=groupBounds[0]}
if (groupBounds[1]>ul_y){ul_y=groupBounds[1]}
if (groupBounds[2]>lr_x){lr_x=groupBounds[2]}
if (groupBounds[3]<lr_y){lr_y=groupBounds[3]}
}
}
activeDocument.views[0].zoom = 1;
ScreenSize = activeDocument.views[0].bounds;
ScreenWidth= ScreenSize[2] - ScreenSize[0];
ScreenHeight=ScreenSize[1] - ScreenSize[3];
screenProportion =ScreenHeight/ScreenWidth;
cntrPos = [ul_x,ul_y];
mySelWidth=(lr_x-ul_x);
mySelHeight=(ul_y-lr_y);
cntrPos[0] = ul_x + (mySelWidth/2);
cntrPos[1] = ul_y - (mySelHeight/2);
activeDocument.views[0].centerPoint = cntrPos;
zoomFactorW = ScreenWidth/mySelWidth;
zoomFactorH = ScreenHeight/mySelHeight;
if((mySelWidth*screenProportion) >= mySelHeight){
zF = zoomFactorW;
}else{
zF = zoomFactorH;
}
activeDocument.views[0].zoom = zF *.85;
}else{
activeDocument.activeView.zoom=1;
}
}