Absolute artboard coordinates

AiScripts
3 min readJun 17, 2022

--

Artboards in an Adobe Illustrator document are on the canvas. The canvas has an absolute size of 16383×16383 px. If you try to move the artboard outside the canvas bounds, you get the error “Artboard can not be created outside the canvas bounds”.

In the API, artboards have only one artboardRect property, which contains the coordinates of the top left corner and the botom right corner. You don’t know where the artboard is on the canvas. If we move artboards along the canvas in the script, we need absolute coordinates so that we don’t get an error or have to use try...catch.

Coordinate of top left corner of artboard on canvas unknown

A trick to get the absolute coordinates of the artboard:

  1. Add an empty pathItem to the document. In Illustrator, it is created above and to the left of the edge of the canvas by default.
  2. Calculate the offset of pathItem from the top left corner of the canvas and create a rectangle (A).
  3. Create a rectangle (B) to the size of the artboard.
  4. The distance from the top left corner A to the same corner B will be the absolute coordinate of the artboard corner.
  5. Knowing the width and height of the artboard, you can calculate the coordinates of the other corners.
Create temporary rectangles
var abCoord = getArtboardCoord(0);
function getArtboardCoord(idx) {
var doc = app.activeDocument;
var aLayer = doc.activeLayer;
var thisAbRect = doc.artboards[idx].artboardRect;
var fakePath = aLayer.pathItems.add();
var cnvsDelta = 1 + ((fakePath.position[0] * 2 - 16384) - (fakePath.position[1] * 2 + 16384)) / 2;
var pathPos = [fakePath.position[0] - cnvsDelta, fakePath.position[1] + cnvsDelta];
var cnvsPath = aLayer.pathItems.rectangle(pathPos[0], pathPos[1], 300, 300);
cnvsPath.filled = false;
cnvsPath.stroked = false;
// Create a rectangle with the same size as the artboard
var top = thisAbRect[1];
var left = thisAbRect[0];
var width = thisAbRect[2] - thisAbRect[0];
var height = thisAbRect[1] - thisAbRect[3];
var abPath = aLayer.pathItems.rectangle(top, left, width, height);
abPath.stroked = false;
abPath.filled = false;
var absLeft = Math.floor(abPath.position[0] - cnvsPath.position[0]);
var absTop = Math.floor(cnvsPath.position[1] - abPath.position[1]);
var absBottom = absTop + height;
var absRight = absLeft + width;
fakePath.remove();
abPath.remove();
cnvsPath.remove();
redraw();
return { 'left': absLeft, 'right': absRight, 'top': absTop, 'bottom': absBottom };
}
  • abCoord — absolute distances to each side of the artboard.
  • cnvsDelta — offset of the empty pathItem from the canvas.
  • cnvsPath — rectangle created in the upper left corner of the canvas.
  • abPath — rectangle of the artboard size with index idx.
Demo

I used this method in the MoveArtboards and DuplicateArtboards scripts. In MoveArtboards, the function prevents the user from moving the artboards beyond the borders of the canvas. In DuplicateArtboards prevents the user from creating more copies than allowed.

--

--

AiScripts

Sergey Osokin. Product Illustrator, Icon Designer, Script Developer (Ai, Ps). Writing about bugs and tricks in Adobe Illustrator scripts