Skip to main content
Inspiring
January 26, 2023
Answered

Artboard size

  • January 26, 2023
  • 3 replies
  • 1165 views

What´s the error code below... The artboard size no change.

 

var doc = app.activeDocument;
var allPathItems = doc.pathItems;
for (var i = 0; i < allPathItems.length; i++) {
allPathItems[i].selected = true;
}
var sel = doc.selection;
var left = sel[0].left;
var top = sel[0].top;
var right = sel[0].right;
var bottom = sel[0].bottom;
for (var i=1; i<sel.length; i++) {
left = Math.min(left, sel[i].left);
top = Math.min(top, sel[i].top);
right = Math.max(right, sel[i].right);
bottom = Math.max(bottom, sel[i].bottom);
}
doc.artboards[0].artboardRect = [left-20, top-20, right+20, bottom+20];

This topic has been closed for replies.
Correct answer jduncan

Start here to get an idea of how to proceed with your script.

3 replies

jduncan
Community Expert
jduncanCommunity ExpertCorrect answer
Community Expert
January 26, 2023

Start here to get an idea of how to proceed with your script.

Inspiring
January 26, 2023

Thank you so much @jduncan 

pixxxelschubser
Community Expert
Community Expert
January 26, 2023

Hi @Julio Ricardo 

I see a bigger problem with your code. It will only work in the end if it is ALWAYS only about pathItems. As soon as compoundPaths, clipping groups, nested groups or other items or effects are present, the script will generate new errors.

 

Question:
How are your files constructed and what exactly is your goal? Fitting the artboard to a selection? Or to the whole document content? Or, or, or ...?

 

Btw - this topic has been discussed here (and in other) forums quite often. So it could well be that there is already a solution to your question.

Inspiring
January 26, 2023

that all objects are selected and that the artboard is larger 10mm for each side (top, bottom, top and bottom)

pixxxelschubser
Community Expert
Community Expert
January 26, 2023

Always remember: scripts are stupid. They only do what they were written for. However, they always do that well.

 

For this reason, an exact description is extremely important.

 

What elements are in your document? Do you have only one artboard? Or several? Should only one artboard be adapted to its elements? Which one? The active one?

 

Or several artboards to their respective content? Or should a new "superartboard" be created over all elements of all artboards? Or, or, or ...?

 

My script snippet in the post linked by @jduncan  (written in 2021) enlarges the active artboard 10mm around the content of the artboard. But it has the restriction that for example live text is only considered with the size of its selection - and not within the limits of its visible bounds.

CarlosCanto
Community Expert
Community Expert
January 26, 2023

to start, sel[0].right and sel[0].bottom return NaN (not a number) since right and bottom properties do not exist

 

to get right and bottom coordinates, use Left coordinate + Selection Width. Similarly Top coordinate - Selection Height

Inspiring
January 26, 2023

Hi @CarlosCanto 

Could you show me?