Skip to main content
Eduard Buta
Inspiring
March 19, 2022
Answered

Script for applying an artboard-sized clipping mask to selection?

  • March 19, 2022
  • 1 reply
  • 925 views

Hello, community.

 

I've been trying to find a script that applies an artboard-sized clipping mask to a selected element. I've found some older scripts here on the forum, but none seemed to work anymore.

 

Does anyone have a more recent working solution to this problem of mine? I'd appreciate a share 🙂

This topic has been closed for replies.
Correct answer femkeblanco

Not every eventuality will be anticipated in the casual script. This will do most of what I assume you want (multiple items per multiple artboards), but, for example, I have not considered an item spanning two artboards:

 

// select items
var doc = app.activeDocument;
var ABs = doc.artboards;
for (var i = 0; i < ABs.length; i++) {
    var AB = ABs[i].artboardRect;
    var maskedArtwork = [];
    for (var j = 0; j < doc.selection.length; j++) {
        var b1 = doc.selection[j].geometricBounds;
        if ((b1[2] > AB[0] && b1[0] < AB[2]) && (b1[3] < AB[1] && b1[1] > AB[3])) {
            maskedArtwork.push(doc.selection[j]);
        }
    }
    if (maskedArtwork.length == 0) {
        continue;
    }
    var clippingPath = doc.pathItems.rectangle(AB[1], AB[0], AB[2] - AB[0], - (AB[3] - AB[1]));
    var clippingSet = app.activeDocument.groupItems.add();
    for (var k = 0; k < maskedArtwork.length; k++) {
        maskedArtwork[k].moveToEnd(clippingSet);
    }
    clippingPath.moveToBeginning(clippingSet);
    clippingSet.clipped = true;
}

 

1 reply

femkeblanco
Legend
March 19, 2022

Do you mean something like this?

 

// select item
var doc = app.activeDocument;
if (doc.selection.length > 0) {
    var AB = doc.artboards[0].artboardRect;
    var clippingPath = doc.pathItems.rectangle(0, 0, AB[2] - AB[0], - (AB[3] - AB[1]));
    var maskedArtwork = doc.selection[0];
    var clippingSet = app.activeDocument.groupItems.add();
    maskedArtwork.moveToBeginning(clippingSet);
    clippingPath.moveToBeginning(clippingSet);
    clippingSet.clipped = true;
} else {
    alert("Select item.")
}

 

Eduard Buta
Inspiring
March 19, 2022

I was looking for exactly this. Thank you very much!