Skip to main content
Participant
October 11, 2021
Answered

Write a script for illustrator to fit artboard to selected items.

  • October 11, 2021
  • 4 replies
  • 3897 views

Hello, 

 

I am searching for a sollution to write a script that scales the artboard in Illustrator. 

What I need is a script that does as follow: 

I make my artwork en select everything. 

From this selection, the script wil adjust the size of the artboard. 

The artboard will have the same size as the artwork but with an extra margin around it. 

For example 10mm above, under, left and right from the artwork. 

 

This is what I have so far, but it only shrinks in the width:

 

var docRef = app.activeDocument;

var index = docRef.artboards.getActiveArtboardIndex();
pdtRedim(index,docRef);

function pdtRedim(idx,doc){
var ab, rect0, rect;
ab = doc.artboards[idx];
rect0 = ab.artboardRect;
doc.fitArtboardToSelectedArt(idx);
rect = ab.artboardRect;
rect = [rect[0],rect0[1],rect[2],rect0[3]]
ab.artboardRect = rect;
}

 

Thank you in advance!

 

This topic has been closed for replies.
Correct answer pixxxelschubser

Something like that?

 

// https://community.adobe.com/t5/illustrator-discussions/write-a-script-for-illustrator-to-fit-artboard-to-selected-items/m-p/12444820#M294237
// thread: Write a script for illustrator to fit artboard to selected items. (means here: select all elements on the active artboard and fit artboard plus offset).
var aDoc = app.activeDocument;
var offset = 10 * 2.83465;
aDoc.selection = null;
var idx = aDoc.artboards.getActiveArtboardIndex();
aDoc.selectObjectsOnActiveArtboard();
aDoc.fitArtboardToSelectedArt(idx); // does not work for visible bounds of editable fonts
var rect = aDoc.artboards[idx].artboardRect;
aDoc.artboards[idx].artboardRect = [rect[0] - offset, rect[1] + offset, rect[2] + offset, rect[3] - offset];
aDoc.selection = null;

 

4 replies

pixxxelschubser
Community Expert
Community Expert
October 12, 2021

10 × conversion factor millimeter to point

 

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
October 11, 2021

Something like that?

 

// https://community.adobe.com/t5/illustrator-discussions/write-a-script-for-illustrator-to-fit-artboard-to-selected-items/m-p/12444820#M294237
// thread: Write a script for illustrator to fit artboard to selected items. (means here: select all elements on the active artboard and fit artboard plus offset).
var aDoc = app.activeDocument;
var offset = 10 * 2.83465;
aDoc.selection = null;
var idx = aDoc.artboards.getActiveArtboardIndex();
aDoc.selectObjectsOnActiveArtboard();
aDoc.fitArtboardToSelectedArt(idx); // does not work for visible bounds of editable fonts
var rect = aDoc.artboards[idx].artboardRect;
aDoc.artboards[idx].artboardRect = [rect[0] - offset, rect[1] + offset, rect[2] + offset, rect[3] - offset];
aDoc.selection = null;

 

Participating Frequently
May 31, 2024

There is an instance that this doesnt work and that's when an object is not on an artboard. Is there a way to select all objects whether on artbaord or not?

Sergey Osokin
Inspiring
May 31, 2024

Select all objects. Temporarily group them. The group will contain objects both on and off the artboard. Run the script. When it's finished, ungroup them... Or run this modification 🙂

var aDoc = app.activeDocument;
var offset = 10 * 2.83465;
aDoc.selection = null;
var idx = aDoc.artboards.getActiveArtboardIndex();
app.executeMenuCommand("selectall"); // Select all in doc. Works in Ai CS6+
aDoc.fitArtboardToSelectedArt(idx); // does not work for visible bounds of editable fonts
var rect = aDoc.artboards[idx].artboardRect;
aDoc.artboards[idx].artboardRect = [rect[0] - offset, rect[1] + offset, rect[2] + offset, rect[3] - offset];
aDoc.selection = null;

 

femkeblanco
Legend
October 11, 2021
// select items
var offset = 10 * 2.83465;
app.executeMenuCommand("group");
var group = app.activeDocument.selection[0];
var AB = app.activeDocument.artboards[0]
AB.artboardRect = [
    group.left - offset, group.top + offset,
    group.left + group.width + offset, group.top - group.height - offset
];
app.executeMenuCommand("ungroup");
Met1
Legend
October 11, 2021