0

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.

  • It's because the coordinate system is different in scripting, the bottom left of your artboard is at 0,0. Is there a reason you need it different? – Cai Sep 11 '16 at 6:38
  • I'm well aware of that. I'm creating multiple artboards depending on the images in a folder, and filling the artboards with them. Using a "different" coordinate system complicates the process of position calculation for the artboards and the images. I'm already working on a script version of "Fit All on Window" command. – akinuri Sep 11 '16 at 9:04
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.