Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Participant ,
Mar 19, 2022 Mar 19, 2022

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 🙂

TOPICS
Scripting
837
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Mar 19, 2022 Mar 19, 2022

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;
   
...
Translate
Adobe
Guide ,
Mar 19, 2022 Mar 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.")
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 19, 2022 Mar 19, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 19, 2022 Mar 19, 2022

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 19, 2022 Mar 19, 2022

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 19, 2022 Mar 19, 2022
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines