Skip to main content
This topic has been closed for replies.
Correct answer m1b

sir how can i get the Canvas size by jsx. thank you


There isn't a straightforward way, but I learned this technique from @OMOTI who discovered it and shared it, so thanks to them!

 

Here it is:

/**
 * Returns bounds of canvas,
 * ie. the largest artboard
 * size possible.
 * @author OMOTI
 * @discussion https://community.adobe.com/t5/illustrator-discussions/the-initial-value-of-mvaluetx-and-mvaluety/m-p/9646654
 * @returns {Array<Number>} - [left, top, right, bottom].
 */
function getCanvasBounds() {
    var tempLayer,
        tempText,
        left,
        top,
        LARGEST_SIZE = 16383;
    if (!app.documents.length) {
        return;
    }
    tempLayer = app.activeDocument.layers.add();
    tempText = tempLayer.textFrames.add();
    left = tempText.matrix.mValueTX;
    top = tempText.matrix.mValueTY;
    tempLayer.remove();
    return new Rect(
        left,
        top,
        left + LARGEST_SIZE,
        top + LARGEST_SIZE
    );
}

// example usage
var rect = getCanvasBounds();

// cover the whole area
app.activeDocument.pathItems.rectangle(rect[1], rect[0], 16383, 16383);

 

1 reply

m1b
Community Expert
Community Expert
March 5, 2024

I tried your code and it worked as expected.

var doc = app.activeDocument;
var rect = [-7211, 7411, -7111, 7211];
doc.artboards.add(rect);

Have you tried it with a different document open? eg. a new document with 1 default artboard?

Also just for troubleshooting, does it stop erroring if you move the position? eg.

 

var doc = app.activeDocument;
var rect = [-5211, 5411, -5111, 5211];
doc.artboards.add(rect);

 

 

- Mark

Known Participant
March 5, 2024

@m1b  My Ai is Illustrator 2022 26.5, I can't add a artboard on any position.