Skip to main content
Participating Frequently
April 30, 2025
Answered

Java Script Help

  • April 30, 2025
  • 2 replies
  • 1759 views

Hi Guys

 

can you please help me out with a specific script that i am very much would like to use for a project that consist of multiple objects 'stacked' / nested ontop of each other thats in different sizes. this has no fill color only outlines, im trying to create or tried looking for alt to clip mask a object that consist of complex artwork.

 

right now i have to clip mask the artwork layer into all 1-11 layers manually which is tedious, also the same artwork has to go onto other parts of the project that also have 1-11 layers. 😩

 

Correct answer m1b

Hi @m1b 

 

As per our discussion, see attached the before and after .ai file.

 

what i basically want is for the script to do, is to be able to clip mask anything be it grouped objects, clip masks or raster images. would you be able to adjust it to a all in one script or does it have to be three different ones.

 

a million tx for this. 


Hi @peter__4654 here is a version that puts any kind of page item into a new clipping mask, using the selected paths. It seems to work okay on your sample document(s).

- Mark

 

/**
 * @file Multi Clip Items.js
 *
 * Usage:
 *   1. Select a bunch of overlapping path items with the
 *      "focus item" at the bottom (can be any type of item).
 *   2. Run script.
 *
 * For each selected path item, will create a clipping group
 * and insert the selected path item(s) as the new mask and
 * the "focus item" as the clipped contents.
 *
 * @author m1b
 * @version 2025-05-16
 * @discussion https://community.adobe.com/t5/illustrator-discussions/java-script-help/m-p/15297349
 */
(function () {

    var doc = app.activeDocument,
        items = doc.selection;

    if (
        !items
        || items.length < 2
    )
        return alert("Please select path items with the 'contents' page item at the bottom.");

    // this is going to be the contents of the new clipping group
    var focusItem = selection.pop();

    for (var i = items.length - 1; i >= 0; i--) {

        var mask = items[i];

        if ('PathItem' != mask.constructor.name)
            // we only want path items (to use for clipping masks)
            continue;

        // duplicate the focus item
        var contentsItem = focusItem.duplicate(mask, ElementPlacement.PLACEAFTER),
            newClippingGroup = mask.layer.groupItems.add();


        // move the path item into the clipping group
        mask.move(newClippingGroup, ElementPlacement.PLACEATBEGINNING);
        contentsItem.move(newClippingGroup, ElementPlacement.PLACEATEND)

        // set up the new clipping mask
        newClippingGroup.clipped = true;
        mask.clipping = true;

    }

    // remove the original focus item
    focusItem.remove();

})();

 

2 replies

Participating Frequently
April 30, 2025

Thanks for the reply Mark.

 

ideally i want it to look like this after the scipt, any help would really help. or guide me how to achieve this. 

m1b
Community Expert
Community Expert
April 30, 2025

Hi @peter__4654, okay well I think to match your demo document, you are duplicating the clipping group to each of the selected path items and then replacing it's clipping mask path with the item. Give this script a try and see if it's close to what you need.

- Mark

/**
 * @file Multi Clip With Path Items.js
 *
 * Usage:
 *   1. Select a bunch of overlapping path items
 *      with a clipping group at the bottom.
 *   2. Run script.
 * 
 * For each selected path item, will duplicate
 * the clipping group and insert the path item
 * as the new mask.
 *
 * @author m1b
 * @version 2025-04-30
 * @discussion https://community.adobe.com/t5/illustrator-discussions/java-script-help/m-p/15297349
 */
(function () {

    var doc = app.activeDocument,
        items = doc.selection;

    if (
        !items
        || items.length < 2
        // make sure the bottom item is a clipping group
        || !items[items.length - 1].clipped
    )
        return alert("Please select path items, with an existing clipping mask at the bottom.");

    var clippingGroup = items[items.length - 1];

    for (var i = items.length - 2; i >= 0; i--) {

        if ('PathItem' != items[i].constructor.name)
            continue;

        var newMask = items[i];

        // duplicate the bottom item which is the existing clipping group
        var dup = clippingGroup.duplicate(newMask, ElementPlacement.PLACEAFTER);

        // copy appearance from the clipping mask to the new mask
        copyAppearance(clippingGroup.pathItems[0], newMask);

        // move the path item into the clipping group
        newMask.move(dup.pathItems[0], ElementPlacement.PLACEAFTER);

        // remove the existing mask item
        dup.pathItems[0].remove();

        // set the new clipping mask
        dup.pathItems[0].clipping = true;

    }

    clippingGroup.remove();

})();

/**
 * Copy appearance properties form on page item to another.
 * @version 2025-04-30
 * @param {PageItem} source - the item to copy from
 * @param {PageItem} destination - the item to copy to
 */
function copyAppearance(source, destination) {

    // NOTE: add in any other properties you want to copy

    destination.filled = source.filled;
    destination.fillColor = source.fillColor;
    destination.strokeColor = source.strokeColor;
    destination.strokeWidth = source.strokeWidth;
    destination.stroked = source.stroked;

    if (source.opacity)
        destination.opacity = source.opacity;

    if (source.blendingMode)
        destination.blendingMode = source.blendingMode;

};

Edit 2025-04-30: added copying of some appearance properties from the clipping mask to the new masks.

Participating Frequently
April 30, 2025

Thank you so much @m1b  it worked!!! i cant thank you enough for this. is it possible to have the master clipping group that is being left behind(left-over) removed, also to have its fill properties copied as well?

m1b
Community Expert
Community Expert
April 30, 2025

Hi @peter__4654 this would be a good question to also post an Illustrator document showing before the script and after the script—in other words what you ideally want it to be like after the script runs.

- Mark

 

P.S. due to forum weird constraints you must post Illustrator pdf files, not .ai. You can just change the extension or save as pdf with Illustrator compatibility.