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

Rotate artboard 180 degrees

Participant ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

Hello community,

 

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

 

please help 

TOPICS
Feature request , Scripting , Tools

Views

1.2K

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 2 Correct answers

Community Expert , Jun 28, 2022 Jun 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°
 *
...

Votes

Translate

Translate
Enthusiast , Jul 04, 2022 Jul 04, 2022

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

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

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

 

 

 

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
Participant ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

@m1b Works like charm. Thank you.

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
Participant ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

I just got an error

var doc = ab.parent,

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 ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

Hi @MidoSemsem, what error did you get? Can you take screenshot? Also if it keeps happening, can you post a sample file with the erroring artboard. Then I can fix it.

- 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
Participant ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

Thanks Mark @m1b 

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 ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

Hi @MidoSemsem, thanks for the sample file. I ran script and it worked as expected and threw no errors. I wonder what is different about your set up. What version of Illustrator are you using? Which operating system. I cannot comprehend how that particular line would throw an error.

- 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
Community Expert ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

I have made some tiny edits to the script, just to hopefully make it easier to see where the error is happening. Can you please try again using the edited script?

- 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
Participant ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

@m1b  The script did really work with some files, but unfortunately the same problem occurred with another file.
I'm Using Windows 10, Adobe Illustrator 2022

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
Participant ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

Samples

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 ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

Okay this time I can reproduce the error you are getting! It is a bug in Illustrator—in this case sometimes it fails to get the parent of the artboard. I only need the parent because I want the document. So I've adjusted the script a little to send in the document another way and it fixed it for me. Please try the above script again and see if the errors go away for you, too.

- 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
Participant ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

Perfect..
Thanks a lot.

I really appreciate your help. 

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
Guide ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

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

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
Participant ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

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

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 ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

Hi @femkeblanco, the keystone of my approach is to rotate the items without reference to their bounds. I made the rotateItemAroundPoint for this reason. Your method implicitly uses the temporary group's bounds by referencing the center transform position. This is a great idea, but it doesn't find the correct centre if the item is partially off the artboard. You could improve the method by making your temp rectangle much larger—but still centred on the artboard—but that is a bit of a hack I suppose.

- 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
Advocate ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

Bonjour à tous,

M1b, votre script est parfait, son avantage par rapport à la solution de femkeblanco réside dans le fait qu'aucun élément n'est déplacé dans le panneau calques, alors que la formation d'un groupe entraine le placement de tous les objets sélectionnés dans le même calque.

Je n'ai pas eu le traduction du mot hack ?

Je propose tout de même ma version...

 

// JavaScript Document
// rotate artboard perso.js
// landry René
// Thu, 30 June 2022 16:50:42 GMT
// Rotate the contents of the active artboard by 180°
function rotate180() {
  var doc, idx, ab, rect, tempRect, cab, grp, Bs, cg;
      doc = app.activeDocument;
      idx  = doc.artboards.getActiveArtboardIndex();
      ab  = doc.artboards[idx];
      rect = ab.artboardRect;
      tempRect = doc.pathItems.rectangle(rect[1],rect[0],rect[2]-rect[0],rect[1]-rect[3]);
      cab = centre(rect);
      doc.selectObjectsOnActiveArtboard();
          if (selection.length == 1) {
            tempRect.remove();
            return;
          }
      app.executeMenuCommand("group");
      grp = doc.groupItems[0];
      Bs = grp.geometricBounds;
      cg = centre(Bs);
      grp.rotate(-180);
      grp.translate((cab[0]-cg[0])*2,(cab[1]-cg[1])*2);
      app.executeMenuCommand("ungroup");
      app.selection = null;
      tempRect.remove();
}
// -------
function centre(Bds)
{return [Bds[0]+(Bds[2]-Bds[0])/2,Bds[3]+(Bds[1]-Bds[3])/2];}
// -------
if (app.documents.length) rotate180();

 

René

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
Participant ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

Nice approch @renél80416020 but it groups all objects in the same layer.

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 ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

Thanks @renél80416020, your version solves @femkeblanco's issue in a clever way, but yes the grouping adds a complication.

 

What is "hack"? It has many meanings, so the translation may have given the wrong meaning. In my usage, I mean "a coding practice that works, but isn't using a conventional approach, and is known to not handle every situation." A coder usually makes a "hack" when a conventional approach is missing, and they must try to workaround the shortcoming, without the correct tools. In this example I mentioned making the rectangle larger so that it would encompass any page items. It is a hack because how large would you make it? What if the page items have unhelpful bounds due to clipping or plug-in objects? It probably isn't a 100% reliable approach so I called it a hack. Another example: the function I wrote here is a hack—I wish we could do it in a conventional way!

- 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
Enthusiast ,
Jun 30, 2022 Jun 30, 2022

Copy link to clipboard

Copied

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
ArtboardsRotateWithObjects.gif

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
Participant ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

@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.

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 ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

Hi @Sergey Osokin, thanks I hadn't seen that. Alexander's script goes the extra mile and handles locked page items, etc. Nice!

- 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
Participant ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

@Alexander Ladygin  Indeed it is. Can we modify the code and add 180 degree to the script? @m1b 

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
Participant ,
Jul 03, 2022 Jul 03, 2022

Copy link to clipboard

Copied

@Sergey Osokin How can I change the degrees in the code? 

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
Advocate ,
Jul 04, 2022 Jul 04, 2022

Copy link to clipboard

Copied

erreur ?

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
Participant ,
Jul 04, 2022 Jul 04, 2022

Copy link to clipboard

Copied

@renél80416020 I can't modify Alexander's script. It's the best one yet.

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