Skip to main content
MidoSemsem
Inspiring
June 28, 2022
Answered

Rotate artboard 180 degrees

  • June 28, 2022
  • 3 replies
  • 7077 views

Hello community,

 

Looking for illustrator script to rotate artboard with artwork 180 degrees.

 

please help 

This topic has been closed for replies.
Correct answer Sergey Osokin

I added the 180 degree option. I'm waiting for Alexander to post an update on his GitHub. You can check the update at my link for now https://gist.github.com/creold/5e50f51267d04cdb540479ae3e13c33b

3 replies

Sergey Osokin
Inspiring
July 1, 2022

Alexander Ladygin also had a script for 90 degree rotation of artboards with artwork. You can change the degrees in the code https://github.com/alexander-ladygin/illustrator-scripts#artboardsrotatewithobjects

MidoSemsem
Inspiring
July 1, 2022

@Sergey Osokin  Excellent one, I use it all the time. But I don't know how to add 180 degrees to the code and the dialogUI.

femkeblanco
Legend
June 29, 2022

Here is my attempt.  (It will rotate multiple artboards.)

MidoSemsem
Inspiring
June 29, 2022

Thanks @femkeblanco. But it aligns the artwork with the artboard and moves any object outside inside.

m1b
Community Expert
Community Expert
June 28, 2022

Hi @MidoSemsem, I've written a script that rotates active artboard by 180°. You can configure it to also rotate by 90° or 270°. - Mark

 

 

 

/**
 * Example usage:
 * Rotate active artboard by 180°.
 */

var doc = app.activeDocument,
    myArtboard = doc.artboards[doc.artboards.getActiveArtboardIndex()];
rotateArtboard(doc, myArtboard, 180);



/**
 * Rotates artboard by mutiples of 90°.
 * @author m1b
 * @param {Artboard} ab - an Illustrator Artboard
 * @param {Number} angle - 90°, 180°, 270°
 */
function rotateArtboard(doc, ab, angle) {
    if (
        ab == undefined
        || ab.constructor.name != 'Artboard'
    )
        return;

    if (
        angle == undefined
        || (angle % 90 != 0)
    ) {
        alert('Angle parameter must be multiple of 90 degrees.');
        return;
    }

    var items = itemsOnArtboard(doc, ab),
        center = centerOfBounds(ab.artboardRect);

    if (angle == 90 || angle == 270) {
        var r = ab.artboardRect;
        ab.artboardRect = boundsByCenterWidthHeight(center, -(r[3] - r[1]), r[2] - r[0]);
    }

    for (var i = 0; i < items.length; i++) {
        rotateItemAroundPoint(items[i], center, angle);
    }

}



/**
 * Get the center point of bounds.
 * @param {Array[Number]} bounds - [L, T, R, B]
 * @returns {Array[Number]} [x, y]
*/
function centerOfBounds(bounds) {
    var l = bounds[0], t = bounds[1], r = bounds[2], b = bounds[3],
        x = l + ((r - l) / 2),
        y = t + ((b - t) / 2);
    return [x, y];
};



/**
 * Rotate page item around a point [x, y].
 * @author m1b
 * @param {PageItem} item - an Illustrator page item
 * @param {Array[Number]} point - a point [x, y]
 * @param {Number} angleInDegrees - rotation angle
 */
function rotateItemAroundPoint(item, point, angleInDegrees) {
    // sanity
    if (
        item == undefined
        || typeof item.translate != 'function'
    )
        return;
    if (
        point == undefined
        || !point.hasOwnProperty('1')
    )
        return;
    if (
        angleInDegrees == undefined
    )
        return;

    // move 'point' to the document origin, then rotate, then move back
    item.translate(-point[0], -point[1]);
    item.rotate(angleInDegrees, true, false, false, false, Transformation.DOCUMENTORIGIN);
    item.translate(point[0], point[1]);
}



/**
 * Returns page items on an Illustrator artboard.
 * NOTE: will modify current selection.
 * @author m1b
 * @param {Artboard} ab - an Illustrator Artboard
 * @returns {Array}
 */
function itemsOnArtboard(doc, ab) {
    if (
        ab == undefined
        || ab.constructor.name != 'Artboard'
    )
        return [];

    var found = [],
        index = getArtboardIndexByName(doc, ab.name);

    if (index == -1)
        return [];

    doc.artboards.setActiveArtboardIndex(index);
    doc.selectObjectsOnActiveArtboard();

    for (var i = 0; i < selection.length; i++) {
        found.push(selection[i]);
    }

    return found;
}



/**
 * Returns the index of Named artboard.
 * @author m1b
 * @param {Document} doc - an Illustrator Document
 * @param {String} name - the Artboard name
 * @returns {Number}
 */
function getArtboardIndexByName(doc, name) {
    for (var i = 0; i < doc.artboards.length; i++) {
        if (doc.artboards[i].name == name)
            return i;
    }
    return -1;
}



/**
 * Calculates bounds of width x height centered on a point.
 * @author m1b
 * @param {point} center - a point [x, y]
 * @param {Number} width - width in points
 * @param {Number} height - height in points
 * @returns {Array} [L, T, R, B]
 */
function boundsByCenterWidthHeight(center, width, height) {
    var cx = center[0], cy = center[1];
    var l = cx - (width / 2);
    var t = cy + (height / 2);
    var r = cx + (width / 2);
    var b = cy - (height / 2);
    return [l, t, r, b];
}

 

 

 

MidoSemsem
Inspiring
June 29, 2022

@m1b Works like charm. Thank you.