Skip to main content
Inspiring
October 21, 2024
Answered

Positioning a clipping mask with contents

  • October 21, 2024
  • 1 reply
  • 1954 views

 

 

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 has been closed for replies.
Correct answer jduncan

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

 

1 reply

jduncan
Community Expert
jduncanCommunity ExpertCorrect answer
Community Expert
October 21, 2024

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

 

Inspiring
October 21, 2024

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)?

jduncan
Community Expert
Community Expert
October 21, 2024

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