Copy link to clipboard
Copied
I am writing a script that makes fiducials around my design and to save material etc, I am resizing the artboard to the artwork using:
doc.artboards[0].artboardRect = doc.visibleBounds;
It works wonders, except if I have a clipping mask in the document because the "clipped"/invisible art is included in the visibleBounds it seems. Any suggestions on how to fix this?
Before making the clipping mask
How it looks after the clipping mask is made and I run my script
I have the same issue when deselecting everything and using the Artboards -> Fit To Artwork Bounds. However if I select the art and use the exact same function (not Fit to Selected Art) it does it correctly. So it kinda seems like a bug
Copy link to clipboard
Copied
I ran into the same problem when creating my MatchObjects script. If you click the link you'll find a "getVisibleBounds" function that should help you out.
https://github.com/joshbduncan/adobe-scripts/blob/main/MatchObjects.jsx
Copy link to clipboard
Copied
Hi @jduncan, thanks for the link to your "getVisibleBounds" function. That should be just what @SThornelf needs.
Do you mind me asking, why do you handle CompoundPathItems specially? In my quick testing just now I get correct result simply from CompoundPathItem.geometricBounds. Is there special cases or am I missing something?
- Mark
Copy link to clipboard
Copied
Yep, I was trying to catch a bunch of weird edge cases I had encountered and @Disposition_Dev found something that I had missed (linked here).
He found that in some cases, compound clipping paths could be comprised solely of groupItems which would cause a runtime error when attempting to get the first pathItem in the compound path. What you are referring to is the fix for this. Here's a link to a file that includes a bunch of weird stuff we used for testing the getVisibleBounds function.
Copy link to clipboard
Copied
So you want your artboard to fit the "clipping path" (the path doing the masking)? If so, presuming this is the first path in the first group in the document (going by your image):
doc.artboards[0].artboardRect = doc.groupItems[0].pathItems[0].visibleBounds;
Copy link to clipboard
Copied
this works just fine if the item in question is just a single, regular clipping path. Unfortunately, this doesn't solve the issue of nested clipping paths or clipping paths that are deep inside a group. IMO the only reliable way is to check whether any clipping masks exist and if they do, dig recursively through the object to find the appropriate dimensions.
Copy link to clipboard
Copied
Unless I'm missing something, the getVisibleBounds function should work fine in most cases with nested clipping paths and groups. I've tested it with a lot of different "junk" and it seems to get the right bounds most every time. I'm sure there is some weirdness I've yet to encounter (especially from exported files) but so far it has performed well.
Just so we are all on the same page, here's the link to a little script that is useful for testing the results of getVisibleBounds. It simply draws small crop marks on the artboard where it determines the visible bounds to be for the selected object.
And, here's another test Illustrator file that includes nested clipping paths and groups. Let me know if I'm missing something?
Copy link to clipboard
Copied
no you're right. getVisibleBounds() is great. I was replying to femkeblanco's suggestion about just explicitly using the top level pathItem from the given object.
Copy link to clipboard
Copied
Cool! I was concerned I was missing something obvious, and I didn't want to be putting something on here that didn't work. Thanks!
Copy link to clipboard
Copied
try this:
var doc = app.activeDocument;
var myArtwork = doc.groupItems[0];
doc.selection = null;
myArtwork.selected = true;
app.executeMenuCommand("Fit Artboard to selected Art");
Copy link to clipboard
Copied
I'm new to Javascripts and executeMenuCommand was news to me.
Resizing the artboard is done twice
1. to have a way to place the fiducials
2. resizing it to include the fiducials in the artboard.
Your code (or femkeblanco's code for that matter) does not include the fiducials. Guessing because I have made a separate layer for them, but would love to know for sure
Your code gave me the clues for the absolutely easiest way to do it (in my case at least) :
app.executeMenuCommand("selectall");
app.executeMenuCommand("Fit Artboard to selected Art");
Copy link to clipboard
Copied
ah, yes indeed. had i known you wanted to fit the artboard to all artwork i would have written that. I assumed there was other artwork that you didn't necessarily want to include.
in that case there's actually an even easier way.. we can optimize your code by 50%!! I specifically used "Fit Artboard to selected art" because i thought we were being selective. If you want to just make an artboard around all the art in the document, use this:
app.executeMenuCommand("Fit Artboard to artwork bounds");
Find more inspiration, events, and resources on the new Adobe Community
Explore Now