Copy link to clipboard
Copied
Hello community,
Looking for illustrator script to rotate artboard with artwork 180 degrees.
please help
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Ā°
*
...
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
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];
}
Copy link to clipboard
Copied
@m1b Works like charm. Thank you.
Copy link to clipboard
Copied
I just got an error
var doc = ab.parent,
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
Copy link to clipboard
Copied
Thanks Mark @m1b
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
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
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Perfect..
Thanks a lot.
I really appreciate your help.
Copy link to clipboard
Copied
Here is my attempt. (It will rotate multiple artboards.)
Copy link to clipboard
Copied
Thanks @femkeblanco. But it aligns the artwork with the artboard and moves any object outside inside.
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
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Ʃ
Copy link to clipboard
Copied
Nice approch @renƩl80416020 but it groups all objects in the same layer.
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
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
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.
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
Copy link to clipboard
Copied
@Alexander Ladygin Indeed it is. Can we modify the code and add 180 degree to the script? @m1b
Copy link to clipboard
Copied
@Sergey Osokin How can I change the degrees in the code?
Copy link to clipboard
Copied
erreur ?
Copy link to clipboard
Copied
@renƩl80416020 I can't modify Alexander's script. It's the best one yet.