Skip to main content
MidoSemsem
Inspiring
January 31, 2024
Answered

A script to place and rotate copies of an item on all artboards

  • January 31, 2024
  • 3 replies
  • 997 views

Hello Everyone;


I’m searching for a script to place and rotate copies of an item on all artboards and align them to corners.

I found this script to place an item:

#target illustrator
var doc = app.activeDocument;

var outlineLayer = doc.layers.add();
outlineLayer.name = "Object";
var placedOutline = outlineLayer.placedItems.add();
placedOutline.file = File("~/AppData/Roaming/Adobe/CEP/Object.pdf");
placedOutline.embed();

But I didn't find a way to finish the script.
 

Attached an item and a sample file of the final file.

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer m1b

Hi @MidoSemsem, I'm wondering if using symbols for the corner graphics would be a good idea? Here's a script I wrote that uses a symbol called "Corner" (you can adjust). Let me know if this is helpful. Try it on my attached demo file to see how I set up the symbol. (The symbol points to the top left corner.)

- Mark

 

/**
 * Puts a symbol item into every corner
 * of every artboard of active document.
 * Usage:
 * 1. Create a Symbol in the active document.
 *    If orientation is important, it should
 *    point to the TOP LEFT.
 * 2. Run script.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/a-script-to-place-and-rotate-copies-of-an-item-on-all-artboards/m-p/14391298
 */
(function () {

    try {
        var doc = app.activeDocument,
            symbol = doc.symbols.getByName('Corner');
    } catch (error) {
        return alert('Could not find symbol "Corner". Please create symbol and try again.');
    }

    addSymbolsToArtboardCorners(doc, symbol, doc.activeLayer.groupItems.add());

})();

/**
 * Positions a symbol item (of `symbol`)
 * into every corner of every artboard of `doc`.
 * @author m1b
 * @version 2024-01-31
 * @param {Document} doc - an Illustrator Document.
 * @param {Symbol} symbol - an Illustrator Symbol.
 * @param {Document|Layer|GroupItem} [container] - an Illustrator container, for the symbol items (default: activeLayer).
 */
function addSymbolsToArtboardCorners(doc, symbol, container) {

    container = container || doc.activeLayer;

    for (var i = 0, ab, angle, symbolItem, len = doc.artboards.length; i < len; i++) {

        ab = doc.artboards[i];

        for (var corner = 0; corner < 4; corner++) {

            angle = 360 - (corner * 90);
            symbolItem = container.symbolItems.add(symbol);
            symbolItem.rotate(angle);
            symbolItem.position = getArtboardCornerPosition(ab, corner, symbolItem.width, symbolItem.height);

        }

    }

    /**
     * Returns the position for the artboard corner.
     * @param {Artboard} ab
     * @param {Number} corner - the corner (0 = TL, 1 = TR, 2 = BR, 3 = BL)
     * @param {Number} w - width of symbol item.
     * @param {Number} h - height of symbol item.
     * @returns {Array<Number>}
     */
    function getArtboardCornerPosition(ab, corner, w, h) {

        var r = ab.artboardRect;

        return [
            [r[0], r[1]],
            [r[2] - w, r[1]],
            [r[2] - w, r[3] + h],
            [r[0], r[3] + h],
        ][corner];
    };

};

 

3 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
January 31, 2024

Hi @MidoSemsem, I'm wondering if using symbols for the corner graphics would be a good idea? Here's a script I wrote that uses a symbol called "Corner" (you can adjust). Let me know if this is helpful. Try it on my attached demo file to see how I set up the symbol. (The symbol points to the top left corner.)

- Mark

 

/**
 * Puts a symbol item into every corner
 * of every artboard of active document.
 * Usage:
 * 1. Create a Symbol in the active document.
 *    If orientation is important, it should
 *    point to the TOP LEFT.
 * 2. Run script.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/a-script-to-place-and-rotate-copies-of-an-item-on-all-artboards/m-p/14391298
 */
(function () {

    try {
        var doc = app.activeDocument,
            symbol = doc.symbols.getByName('Corner');
    } catch (error) {
        return alert('Could not find symbol "Corner". Please create symbol and try again.');
    }

    addSymbolsToArtboardCorners(doc, symbol, doc.activeLayer.groupItems.add());

})();

/**
 * Positions a symbol item (of `symbol`)
 * into every corner of every artboard of `doc`.
 * @author m1b
 * @version 2024-01-31
 * @param {Document} doc - an Illustrator Document.
 * @param {Symbol} symbol - an Illustrator Symbol.
 * @param {Document|Layer|GroupItem} [container] - an Illustrator container, for the symbol items (default: activeLayer).
 */
function addSymbolsToArtboardCorners(doc, symbol, container) {

    container = container || doc.activeLayer;

    for (var i = 0, ab, angle, symbolItem, len = doc.artboards.length; i < len; i++) {

        ab = doc.artboards[i];

        for (var corner = 0; corner < 4; corner++) {

            angle = 360 - (corner * 90);
            symbolItem = container.symbolItems.add(symbol);
            symbolItem.rotate(angle);
            symbolItem.position = getArtboardCornerPosition(ab, corner, symbolItem.width, symbolItem.height);

        }

    }

    /**
     * Returns the position for the artboard corner.
     * @param {Artboard} ab
     * @param {Number} corner - the corner (0 = TL, 1 = TR, 2 = BR, 3 = BL)
     * @param {Number} w - width of symbol item.
     * @param {Number} h - height of symbol item.
     * @returns {Array<Number>}
     */
    function getArtboardCornerPosition(ab, corner, w, h) {

        var r = ab.artboardRect;

        return [
            [r[0], r[1]],
            [r[2] - w, r[1]],
            [r[2] - w, r[3] + h],
            [r[0], r[3] + h],
        ][corner];
    };

};

 

MidoSemsem
Inspiring
January 31, 2024

Thanks @m1b 
It works perfectly 

m1b
Community Expert
Community Expert
January 31, 2024

Great!