Copy link to clipboard
Copied
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!
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();
...
Copy link to clipboard
Copied
Copy link to clipboard
Copied
// 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");
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
Yes! It works, Thank you very much!
Copy link to clipboard
Copied
Cool, but what's the 10 * 2.83465 for?
Copy link to clipboard
Copied
1mm = 2.83465 points, so that makes 10mm as the original post dexcribed. I changed it to 72 so I would get 1 inch and it seems to work. Keep in mind it adds to the stroke width of the object.
Copy link to clipboard
Copied
Perfect. Thank you.
Is there a way to make it work for geometric bounds too?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
Thanks, I also ended up figuring out MenuCommand selectall.
Copy link to clipboard
Copied
10 × conversion factor millimeter to point