Skip to main content
Known Participant
June 29, 2021
Answered

Hi everyone, is there a script for batch clipping masks? Thank you

  • June 29, 2021
  • 2 replies
  • 2414 views

I often use scripts to cut masks in batches in my work. I hope someone can help me. Thank you.

This topic has been closed for replies.
Correct answer femkeblanco

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


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

 

 

2 replies

Kurt Gold
Community Expert
Community Expert
July 1, 2021

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?

m1b
Community Expert
Community Expert
June 29, 2021

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.

Known Participant
June 29, 2021

Hello m1b, please see the demo GIF picture, thank you

femkeblanco
femkeblancoCorrect answer
Legend
July 1, 2021

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


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