Skip to main content
Max Mugen
Inspiring
February 13, 2023
Answered

Script Aling to artboard

  • February 13, 2023
  • 1 reply
  • 2084 views

I'm unable to find a way to tell illustrator to use "align to artboard" instead of the default "align to selection" value
is there a way to change that via script or action? 

I'm in the process of creating a long action/script for file prep. It runs hundreds of lines and some scripts, all good. But there I have one step where I have a small issue
One of the steps, create (via script) two text fields on one layer that are sort of on top of each other. 
id like to center them, relative to the artboard, vertically and horizontally. Could not find a way to do that 

Also, I'm unable to find a way to cycle through my selection. 
Let's say I select all (so 2 objects), cycle to the first object > move 100 pt - select the second object move 200 pt. 
could not find a solution either here 

The dropdown menu right-click  select yields nothing with this selection 

Lastly, I thought about creating a dummy object, say a rectangle, and using this object as a reference for "align to object" but once again was unable to find a way to target this specific command. 

Any thoughts on how to do that? 
I'm sure I'm missing something obvious ^^"
 thanks for the help 

here is the script that prints the 2 text fields. because my doc has 2 artboards on top of each other 

 

 

/*
script that print out artboard dimensions in mm as text object and put them in a new layer 
By GPT
*/

// Get the active document
var doc = app.activeDocument;

// Create a new layer named "Artboard Info"
var artboardDimensionsLayer = doc.layers.add();
artboardDimensionsLayer.name = "Artboard Info";

// Loop through all the artboards in the document
for (var i = 0; i < doc.artboards.length; i++) {
// Get the current artboard
var artboard = doc.artboards[i];

// Convert the artboard dimensions from points to millimeters
var widthMM = artboard.artboardRect[2] - artboard.artboardRect[0];
widthMM = widthMM * 0.352778;
var heightMM = artboard.artboardRect[1] - artboard.artboardRect[3];
heightMM = heightMM * 0.352778;

// Create a text frame with the artboard dimensions
var textFrame = artboardDimensionsLayer.textFrames.add();
textFrame.contents = "Artboard Dimensions - " + artboard.name + "\nWidth: " + widthMM.toFixed(2) + " mm\nHeight: " + heightMM.toFixed(2) + " mm";

// Position the text frame in the center of the artboard
textFrame.position = [artboard.artboardRect[0] + widthMM / 2, artboard.artboardRect[3] + heightMM / 2];
}

 

 

 

This topic has been closed for replies.
Correct answer Sergey Osokin
// Get the active document
var doc = app.activeDocument;
var lName = "Artboard Info";
// Create a new layer named "Artboard Info"
var abDimLayer;
try {
  abDimLayer = doc.layers.getByName(lName);
} catch (err) {
  abDimLayer = doc.layers.add();
  abDimLayer.name = lName;
}

var texts = [];
var origCoord = app.coordinateSystem;
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
// Loop through all the artboards in the document
for (var i = 0; i < doc.artboards.length; i++) {
  // Get the current artboard
  var ab = doc.artboards[i];
  doc.artboards.setActiveArtboardIndex(i);

  // Convert the artboard dimensions from points to millimeters
  var abWidth = ab.artboardRect[2] - ab.artboardRect[0];
  var abWidthMM = Math.abs(abWidth * 0.352778);
  var abHeight = ab.artboardRect[3] - ab.artboardRect[1];
  var abHeightMM = Math.abs(abHeight * 0.352778);

  // Create a text frame with the artboard dimensions
  var tf = abDimLayer.textFrames.add();
  tf.contents = "Artboard Dimensions - " + ab.name + "\nWidth: " + abWidthMM.toFixed(2) + " mm\nHeight: " + abHeightMM.toFixed(2) + " mm";

  // Position the text frame in the center of the artboard
  tf.position = [
    abWidth / 2 - tf.width / 2, 
    abHeight / 2 + tf.height / 2
  ];
  texts.push(tf);
}
app.coordinateSystem = origCoord;

// Move TextFrames by Y-axis
var delta = 100; // px
for (var j = 0; j < texts.length; j++) {
  texts[j].translate(0, -delta * (j + 1));
}

1 reply

Sergey Osokin
Sergey OsokinCorrect answer
Inspiring
February 13, 2023
// Get the active document
var doc = app.activeDocument;
var lName = "Artboard Info";
// Create a new layer named "Artboard Info"
var abDimLayer;
try {
  abDimLayer = doc.layers.getByName(lName);
} catch (err) {
  abDimLayer = doc.layers.add();
  abDimLayer.name = lName;
}

var texts = [];
var origCoord = app.coordinateSystem;
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
// Loop through all the artboards in the document
for (var i = 0; i < doc.artboards.length; i++) {
  // Get the current artboard
  var ab = doc.artboards[i];
  doc.artboards.setActiveArtboardIndex(i);

  // Convert the artboard dimensions from points to millimeters
  var abWidth = ab.artboardRect[2] - ab.artboardRect[0];
  var abWidthMM = Math.abs(abWidth * 0.352778);
  var abHeight = ab.artboardRect[3] - ab.artboardRect[1];
  var abHeightMM = Math.abs(abHeight * 0.352778);

  // Create a text frame with the artboard dimensions
  var tf = abDimLayer.textFrames.add();
  tf.contents = "Artboard Dimensions - " + ab.name + "\nWidth: " + abWidthMM.toFixed(2) + " mm\nHeight: " + abHeightMM.toFixed(2) + " mm";

  // Position the text frame in the center of the artboard
  tf.position = [
    abWidth / 2 - tf.width / 2, 
    abHeight / 2 + tf.height / 2
  ];
  texts.push(tf);
}
app.coordinateSystem = origCoord;

// Move TextFrames by Y-axis
var delta = 100; // px
for (var j = 0; j < texts.length; j++) {
  texts[j].translate(0, -delta * (j + 1));
}

Max Mugen
Max MugenAuthor
Inspiring
February 13, 2023

Thak you for your help 🙂 
After some tweaking and testing between the script and the action I was able to get to the desired result. 
Thank you for sharing, I makes us learn. I'm finally done with this process automation. 

Just to clarify for further scripts, can we confirm that there is no direct API for those, hence script is the way to go with some workaround :
- Align to artboard menu (selection, key object, artboard)
- Cycle thru actively selected objects, if there is more than one type. Maybe it is possible via script if you know you have several paths, for example, but I'm not sure.. 

Once again thank you very much 

-- maxmugen.com
Sergey Osokin
Inspiring
February 13, 2023

1) "Align to artboard menu (selection, key object, artboard)" — align menu keys are not available in the scripts.
2) "Cycle thru actively selected objects, if there is more than one type" — let me clarify what you mean. The For loop and similar ones can be written for different arrays.