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

Create Rectangle and Clipping Mask

Explorer ,
Jan 26, 2023 Jan 26, 2023

Copy link to clipboard

Copied

Bar Left = "Bar Left" Layer

Bar Right = "Bar Right" Layer

Rectangle Green = "Job" Layer.

 

How to create a rectangle in the layer "Job" between the two bars, the width of the artboard that is the same as the green rectangle, but the height of the bars and then select the objects of the layer "Job", which would be the green rectangle plus the rectangle created and apply clipping mask ?

Untitled-1.jpg

TOPICS
Scripting

Views

490

Translate

Translate

Report

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

Explorer , Jan 31, 2023 Jan 31, 2023

Hi @m1b 

 

I got it ! ;-D

 

// Get the active document
var doc = app.activeDocument;

// Get the artboard object
var artboard = doc.artboards[doc.artboards.getActiveArtboardIndex()];

// Get the "Arte" layer
var layerArte = doc.layers.getByName("Arte");

// Create a rectangle with the same size as the artboard
var rect = doc.pathItems.rectangle(artboard.artboardRect[1], artboard.artboardRect[0], artboard.artboardRect[2]-artboard.artboardRect[0], artboard.artboardRect[1]-artboard.artboardRect[3]);
rect.move(lay

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 26, 2023 Jan 26, 2023

Copy link to clipboard

Copied

Hi @Julio Ricardo, here's a script I wrote that clips all items on the "Job" layer to the active artboard rectangle. I didn't consider the black bars yet. Did you mean that you wanted the clipping rectangle to only be the height of the black bars, not the artboard height? - Mark

 

/**
 * Group and mask items on layer "Job" to artboard bounds.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/create-rectangle-and-clipping-mask/m-p/13530889
 */
var targetLayerName = 'Job';

(function () {

    if (app.documents.length == 0) {
        alert('Please open a document and try again');
        return;
    };

    var doc = app.activeDocument,
        targetLayer = getThing(doc.layers, 'name', targetLayerName);

    if (targetLayer == undefined) {
        alert('Error: no layer named "' + targetLayerName + '"');
        return;
    }

    var targetArtboard = doc.artboards[app.activeDocument.artboards.getActiveArtboardIndex()];

    var clippedGroup = addClippingMaskToItemsInLayerOnArtboard(doc, targetLayer, targetArtboard);


    /**
     * Groups all page items on layer and clip to artboard bounds.
     * @author m1b
     * @version 2023-01-27
     * @param {Document} [doc] - an Illustrator Document (default: activeDocument).
     * @param {Layer} [targetLayer] - an Illustrator Layer (default: activeLayer).
     * @param {Artboard} [targetArtboard] - an Illustrator Artboard (default: activeArtboard).
     * @return {GroupItem}
     */
    function addClippingMaskToItemsInLayerOnArtboard(doc, targetLayer, targetArtboard) {

        targetLayer = targetLayer || doc.activeLayer;

        var targetArtboard = targetArtboard || app.activeDocument.artboards[app.activeDocument.artboards.getActiveArtboardIndex()],
            clipRectangle = rectanglePathItem(targetArtboard.artboardRect),
            clippedGroup = addClippingMaskToItems(targetLayer.pageItems, clipRectangle, targetLayer);

        if (clippedGroup == undefined)
            // clipping failed so remove the rectangle
            clipRectangle.remove();

    };


    /**
     * Groups all page items on layer and adds clipping mask.
     * @author m1b
     * @version 2023-01-27
     * @param {Array<PageItem>|collection} items - the items to clip.
     * @param {PathItem} clippingItem - the clipping path.
     * @param {Document|Layer} [whereToGroup] - the location for the group (default: activeLayer).
     * @returns {GroupItem} - the clipped group.
     */
    function addClippingMaskToItems(items, clippingItem, whereToGroup) {

        if (clippingItem == undefined)
            throw Error('addClippingMaskToItems failed: no clippingItem supplied.');

        var itemsToGroup = [clippingItem];

        for (var i = 0; i < items.length; i++)
            itemsToGroup.push(items[i]);

        if (itemsToGroup.length < 3)
            // not enough items to clip
            return;

        var clippingGroup = group(itemsToGroup, undefined, whereToGroup);

        clippingGroup.clipped = true;
        clippingItem.clipping = true;

        return clippingGroup;

    };


    /**
     * Makes a group and moves items into it.
     * @author m1b
     * @version 2022-07-24
     * @param {Array|collection} items - the Illustrator PageItems to group.
     * @param {Document} [doc] - an Illustrator Document (default: activeDocument).
     * @param {Document|Layer|PageItem} [whereToGroup] - the location for the group (default: normal grouping behaviour).
     * @param {makeDuplicatesOfItems} [makeDuplicatesOfItems] - duplicate instead of move items into group (default: false).
     * @returns {GroupItem}
     */
    function group(items, doc, whereToGroup, makeDuplicatesOfItems) {

        if (
            items == undefined
            || items.length == 0
        )
            throw ('group failed: no items.');

        var doc = doc || app.activeDocument,
            group = doc.groupItems.add();

        if (whereToGroup == undefined)

            // behave like manual group
            group.move(items[0], ElementPlacement.PLACEBEFORE);

        else
            // move the grouped items into the new location
            group.move(whereToGroup, ElementPlacement.PLACEATBEGINNING);

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

            if (makeDuplicatesOfItems === true)
                items[i].duplicate(group, ElementPlacement.PLACEATBEGINNING);

            else
                items[i].move(group, ElementPlacement.PLACEATBEGINNING);

        }

        return group;

    };


    /**
     * Generic key/value getter.
     * @author m1b
     * @version 2022-08-18
     * @param {Array|collection} things - the things to search.
     * @param {String} key - the property to search for.
     * @param {String} value - the value to search for.
     * @returns {Object} - the found thing.
     */
    function getThing(things, key, value) {

        for (var i = 0; i < things.length; i++)

            if (things[i][key] == value)
                return things[i];

    };


    /**
     * Creates a rectangle path item.
     * @author m1b
     * @version 2021-03-08
     * @param {Array<Number>} rect - [L, T, R, B]
     * @param {Document|Layer|CompoundPathItem} container - the place to put the rectangle (default: activeDocument.activeLayer).
     * @returns {PathItem}
     */
    function rectanglePathItem(rect, container) {

        container = container || app.activeDocument.activeLayer;

        if (!container.hasOwnProperty('pathItems'))
            throw Error('rectanglePathItem failed: container cannot contain pathItems.');

        var left = rect[0],
            top = rect[1],
            right = rect[2],
            bottom = rect[3],

            newpath = container.pathItems.add();

        newpath.setEntirePath([
            [left, top],
            [right, top],
            [right, bottom],
            [left, bottom]
        ]);

        newpath.closed = true;

        return newpath;

    };


})();

Votes

Translate

Translate

Report

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
Explorer ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

@m1b nothing happend... 

The code isn´t inverted? From below to up?

1) First create the rectangle into "Job" Layer, between the bars (same height bars)... ( Did you mean that you wanted the clipping rectangle to only be the height of the black bars, not the artboard height? YES )

2) Select all objects that Layer

3) Apply Clipping Mask

4)I'm glad ;-D

 

Thanks for your help man...

 

Votes

Translate

Translate

Report

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
Community Expert ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

Hi @Julio Ricardo, can you send me a test file with the artboard and black bars and layers and sample objects set up? You can post it here if it is saved as Illustrator PDF with "Preserve Illustrator Editing Capabilities". Otherwise it'll take too long to understand why "nothing happened". 🙂

- Mark

Votes

Translate

Translate

Report

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
Explorer ,
Jan 31, 2023 Jan 31, 2023

Copy link to clipboard

Copied

Hi @m1b 

 

I got it ! ;-D

 

// Get the active document
var doc = app.activeDocument;

// Get the artboard object
var artboard = doc.artboards[doc.artboards.getActiveArtboardIndex()];

// Get the "Arte" layer
var layerArte = doc.layers.getByName("Arte");

// Create a rectangle with the same size as the artboard
var rect = doc.pathItems.rectangle(artboard.artboardRect[1], artboard.artboardRect[0], artboard.artboardRect[2]-artboard.artboardRect[0], artboard.artboardRect[1]-artboard.artboardRect[3]);
rect.move(layerArte, ElementPlacement.PLACEATBEGINNING);

//Bring to front the rectangle
rect.zOrder(ZOrderMethod.BRINGTOFRONT);

// Select all objects in the "Arte" layer
doc.activeLayer = layerArte;
var items = layerArte.pageItems;
for (var i = 0; i < items.length; i++) {
items[i].selected = true;
}

// Create a Clipping Mask
app.executeMenuCommand('makeMask');

Votes

Translate

Translate

Report

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
Community Expert ,
Jan 31, 2023 Jan 31, 2023

Copy link to clipboard

Copied

LATEST

Hey @Julio Ricardo that's awesome! Well done. 🙂

 

I'd really like to know what went wrong with my script, so I can improve it. Was there a particular file it failed on?

- Mark

Votes

Translate

Translate

Report

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