• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Oct 11, 2021 Oct 11, 2021

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!

 

TOPICS
Scripting

Views

2.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 11, 2021 Oct 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();
...

Votes

Translate

Translate
Adobe
Mentor ,
Oct 11, 2021 Oct 11, 2021

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 11, 2021 Oct 11, 2021

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");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 11, 2021 Oct 11, 2021

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;

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2021 Oct 11, 2021

Copy link to clipboard

Copied

Yes! It works, Thank you very much! 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Oct 12, 2021 Oct 12, 2021

Copy link to clipboard

Copied

Cool, but what's the 10 * 2.83465 for?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 31, 2024 May 31, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

Perfect. Thank you.

Is there a way to make it work for geometric bounds too?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 31, 2024 May 31, 2024

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 31, 2024 May 31, 2024

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;

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 05, 2024 Jun 05, 2024

Copy link to clipboard

Copied

LATEST

Thanks, I also ended up figuring out MenuCommand selectall.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 12, 2021 Oct 12, 2021

Copy link to clipboard

Copied

10 × conversion factor millimeter to point

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines