New, Print, Letter) the created" /> Mobile app version of vmapp.org
Login or Join
Annie732

: "Fit All in Window" using JavaScript I've recently started scripting in Illustrator and having a little problem. When creating a document through File tab, (File->New, Print, Letter) the created

@Annie732

Posted in: #AdobeIllustrator #IllustratorScripting #Javascript

I've recently started scripting in Illustrator and having a little problem. When creating a document through File tab, (File->New, Print, Letter) the created document is positioned at (0,0) and the view is focused on the document.

But if I create a document using JS, for example app.documents.add();, the created document isn't positioned at (0,0), but at (0,-792). I reposition the artboard and move it to (0,0) with app.activeDocument.artboards[0].artboardRect = [0, 0, 612, -792];. This moves the artboard below, out of the view. I need to scroll down to get the artboard into view.

Is there a way to focus the view on the artboard using JS? Maybe call the "Fit All in Window" command?

If there isn't a quick fix, I'll have to do this the hard way, code it myself.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Annie732

1 Comments

Sorted by latest first Latest Oldest Best

 

@Berumen635

I've been told [*] that there is a built-in way to do this:

app.executeMenuCommand("fitall");


but I've created a JS version of the command anyway.

"Fit All in Window" command for Illustrator in JavaScript

function calcRect(rectArray) {
var rect = {
rect : rectArray.join(", "),
topLeft : {
x : rectArray[0],
y : rectArray[1],
},
bottomRight : {
x : rectArray[2],
y : rectArray[3],
},
};
rect.width = Math.abs(rect.bottomRight.x - rect.topLeft.x);
rect.height = Math.abs(rect.bottomRight.y - rect.topLeft.y);
rect.center = [
rect.topLeft.x + (rect.width / 2),
rect.topLeft.y - (rect.height / 2)
];
rect.aspectRatio = rect.width / rect.height;
return rect;
}

function calcViewRect(rectArray, zoom) {
var rect = {
rect : rectArray.join(", "),
topLeft : {
x : rectArray[0],
y : rectArray[1],
},
bottomRight : {
x : rectArray[2],
y : rectArray[3],
},
};
rect.width = Math.abs(rect.bottomRight.x - rect.topLeft.x);
rect.height = Math.abs(rect.bottomRight.y - rect.topLeft.y);
rect.zoom = zoom || 1;
rect.actualWidth = rect.width * zoom;
rect.actualHeight = rect.height * zoom;
rect.aspectRatio = rect.width / rect.height;
return rect;
}

function getDocumentBounds(artboards) {
var rect = {
topLeft : {
x : null,
y : null,
},
bottomRight : {
x : null,
y : null,
},
};

for (var i = 0; i < artboards.length; i++) {
var artboardRect = calcRect(artboards[i].artboardRect);

if (rect.topLeft.x == null) {
rect.topLeft.x = artboardRect.topLeft.x;
} else {
if (artboardRect.topLeft.x < rect.topLeft.x) {
rect.topLeft.x = artboardRect.topLeft.x;
}
}

if (rect.topLeft.y == null) {
rect.topLeft.y = artboardRect.topLeft.y;
} else {
if (artboardRect.topLeft.y > rect.topLeft.y) {
rect.topLeft.y = artboardRect.topLeft.y;
}
}

if (rect.bottomRight.x == null) {
rect.bottomRight.x = artboardRect.bottomRight.x;
} else {
if (artboardRect.bottomRight.x > rect.bottomRight.x) {
rect.bottomRight.x = artboardRect.bottomRight.x;
}
}

if (rect.bottomRight.y == null) {
rect.bottomRight.y = artboardRect.bottomRight.y;
} else {
if (artboardRect.bottomRight.y < rect.bottomRight.y) {
rect.bottomRight.y = artboardRect.bottomRight.y;
}
}
}
rect.rect = [rect.topLeft.x, rect.topLeft.y, rect.bottomRight.x, rect.bottomRight.y];
return rect;
}

function calcZoom(viewRect, documentRect, margin) {
if (documentRect.aspectRatio > viewRect.aspectRatio) {
return parseFloat(((viewRect.actualWidth - (2 * margin)) / documentRect.width).toFixed(2));
} else {
return parseFloat(((viewRect.actualHeight - (2 * margin)) / documentRect.height).toFixed(2));
}
}

function fitAll(document) {
var view = document.views[0];

var viewRect = calcViewRect(view.bounds, view.zoom);
var documentRect = calcRect(getDocumentBounds(document.artboards).rect);

view.centerPoint = documentRect.center;
view.zoom = calcZoom(viewRect, documentRect, 20);
}

// fitAll(app.activeDocument);

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme