Copy link to clipboard
Copied
I think I overthought things. The following should make a clipping mask of each two overlapping selected items, whatever the masked artwork is (group, compound path, et cetera). See if it works for you.
var selectedItems = app.activeDocument.selection;
var a = [];
var test = function(path1, path2) {
var b1 = path1.geometricBounds;
var b2 = path2.geometricBounds;
return (b1[2] > b2[0] && b1[0] < b2[2]) && (b1[3] < b2[1] && b1[1] > b2[3]);
}
for (var i = 0; i < selectedItems.length ;
...
Copy link to clipboard
Copied
Just to help clarify, could you post screenshot of the expanded layers palette before and after the process? Expand the layer so we can see each page item—one before the action and once again afterwards. Thanks.
Copy link to clipboard
Copied
Hello m1b, please see the demo GIF picture, thank you
Copy link to clipboard
Copied
Is the plan to target the items by selecting them?
Copy link to clipboard
Copied
Determine the goal by selecting the project Thank you
Copy link to clipboard
Copied
For the simplest case, where items are path items (i.e. not group items or compound path items), the following makes a clipping mask of each two overlapping selected items:
var selectedItems = app.activeDocument.selection;
var a = [];
var test = function(path1, path2) {
var b1 = path1.geometricBounds;
var b2 = path2.geometricBounds;
return (b1[2] > b2[0] && b1[0] < b2[2]) && (b1[3] < b2[1] && b1[1] > b2[3]);
}
for (var i = 0; i < selectedItems.length ; i++) {
for (var j = 0; j < selectedItems.length ; j++) {
if (j != i && test(selectedItems[i], selectedItems[j])) {
if (!selectedItems[i].done || !selectedItems[j].done) {
var x = [];
x.push(selectedItems[i]);
x.push(selectedItems[j]);
a.push(x);
selectedItems[i].done = true;
selectedItems[j].done = true;
if (x[0].zOrderPosition < x[1].zOrderPosition) {
var temp = x[0];
x[0] = x[1];
x[1] = temp;
}
}
}
}
}
for (var i = 0; i < a.length; i ++) {
var group = app.activeDocument.groupItems.add();
a[i][0].moveToEnd(group);
a[i][1].moveToEnd(group);
group.clipped = true;
}
For a more comprehensive approach, you could try groupOverlappingObjects by John Wundes, adding the following right at the end of the script:
for (var i = 0; i < app.activeDocument.groupItems.length; i++) {
app.activeDocument.groupItems[i].clipped = true;
}
Copy link to clipboard
Copied
Hello femke blanco, I don’t know how to write scripts. Will the code of group projects and compound path projects be complicated? Thank you
Copy link to clipboard
Copied
I think I overthought things. The following should make a clipping mask of each two overlapping selected items, whatever the masked artwork is (group, compound path, et cetera). See if it works for you.
var selectedItems = app.activeDocument.selection;
var a = [];
var test = function(path1, path2) {
var b1 = path1.geometricBounds;
var b2 = path2.geometricBounds;
return (b1[2] > b2[0] && b1[0] < b2[2]) && (b1[3] < b2[1] && b1[1] > b2[3]);
}
for (var i = 0; i < selectedItems.length ; i++) {
if (!selectedItems[i].done) {
for (var j = 0; j < selectedItems.length ; j++) {
if (j != i && test(selectedItems[i], selectedItems[j])) {
var x = [];
x.push(selectedItems[i]);
x.push(selectedItems[j]);
a.push(x);
selectedItems[j].done = true;
}
}
}
}
for (var i = 0; i < a.length; i ++) {
var group = app.activeDocument.groupItems.add();
a[i][0].moveToEnd(group);
a[i][1].moveToEnd(group);
group.clipped = true;
}
Copy link to clipboard
Copied
Thank you femkeblanco, this is exactly what I want, very good
Copy link to clipboard
Copied
Your approach is what im looking for. Just got over this post been looking this for allmost 10 years work arround, but the script doesn't seem to work. Tried to click paths, images in a batch way but it isn't working. I have now 2022, hope this can be fixed or other wise explain how to use it. Litteraly is a must on Illustrator.
GEtting this error https://postimg.cc/NLQJf8kM
Thanks for the script.
Copy link to clipboard
Copied
The script does not manage layers. The error message implies a layer, presumably the first, is either locked or invisible. Try changing the fifth line from the bottom from
var group = app.activeDocument.groupItems.add();
to
var group = a[i][0].parent.groupItems.add();
To use, just select the items in question and run.
Copy link to clipboard
Copied
Works perfect... This script is gold man ❤️
Copy link to clipboard
Copied
It may be pretty useful if you were providing a couple of Illustrator sample files including some clear instructions that may explain what exactly you are going to do.
Also, which version of Illustrator are you using?