Skip to main content
Inspiring
Oct 21, 2024 2:21 PM UTC
Answered

Positioning a clipping mask with contents

 

 

var item = doc.pageItems[i];
var itemWidth = item.width;
var newX = (docWidth - itemWidth) / 2;
var newY = 0;
item.position = [newX, newY];

 

 

When the items are simple shapes the above script works as expected i.e. the top border aligned at top artboard side. In case the items are cplipping mask with contents which is higher than the mask then the script fails i.e. the top artbord side aligned with top mask contents. What is possible to do to fix it? I expect that the clipping mask top border will be aligned at the artboard's top side. 

This topic is closed to new replies. Start a new post to keep the conversation going.
jduncan
jduncanCorrect answer
Community Expert
Oct 21, 2024 3:05 PM UTC

Hey, here's a handy function for getting the bounds of objects that accounts for clipping masks and compound paths. Let me know if you have any questions?

 

GetVisibleBounds: https://github.com/joshbduncan/illustrator-scripts/blob/ada54de802cb950f711f2b78ae93e231a426998c/jsx/utils/GetVisibleBounds.jsxinc

 

9 replies

jduncan
Community Expert
jduncanCommunity ExpertCorrect answer
Community Expert
Oct 21, 2024 3:05 PM UTC

Hey, here's a handy function for getting the bounds of objects that accounts for clipping masks and compound paths. Let me know if you have any questions?

 

GetVisibleBounds: https://github.com/joshbduncan/illustrator-scripts/blob/ada54de802cb950f711f2b78ae93e231a426998c/jsx/utils/GetVisibleBounds.jsxinc

 

Translate
Inspiring
Oct 21, 2024 4:49 PM UTC

As I understand it, the item.position property always places the object on the boundaries of the real content (e.g. what is hidden in the mask)?

Translate
jduncan
Community Expert
Community Expert
Oct 21, 2024 5:14 PM UTC

Correct the position property (and same for the multiple bounds properties). The function linked above returns an array of the provided object bounds [left, top, right, bottom] accounting for the mask. Running the script below in the provided file (with linked function included) presents a dialog with the info from the screenshot. As you can see the hidden ovals are included in the info from the API, whereas the custom function returns what I think you are looking for...

 

var o = app.activeDocument.selection[0];
alert(
  "Placement Info\nPosition: " +
    o.position +
    "\nGeometric Bounds: " +
    o.geometricBounds +
    "\nVisible Bounds: " +
    o.visibleBounds +
    "\ngetVisibleBounds: " +
    getVisibleBounds(o)
);

 

Translate