Copy link to clipboard
Copied
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 🙂
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;
...
Copy link to clipboard
Copied
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.")
}
Copy link to clipboard
Copied
I was looking for exactly this. Thank you very much!
Copy link to clipboard
Copied
Upon further testing, it seems it doesn't work when there are multiple artboards involved. It picks up the bounds of the first artboard only, for every selection I make. Am I doing something wrong, or is the script limited in that sense?
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
This is perfect right now for my use case. Anything more than this would definitely be an overkill at this point. Thank you very much!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now