Copy link to clipboard
Copied
Hi community, Happy new year! Hope you're all doing good.
Can anyone help me with a script (or some threads where I can find information) to do the following.
Let's say I have selected an object / group / vector (let's call it "design"), width and heigth will be variable, sometimes it will be wider sometimes and the way around in other circumstanses. I would like to have an script that will resize and center that design in the middle of a circle / elipse of 18.8mm x 18.8mm , the design will not exceed that area, and after that, the circle should be deleted (because we will not need it anymore) and then center the new resized design in an artboard of 1 inch x 1 inch. Not sure what's teh easiest way if to create a new document or to just resize the actual artboard, i'll rather prefer to resize the actual artboard.
I will appreciat your help! Picture for reference. Please note that the "design" will increase it's size or reduce it's size in order to fit, and I am only showing how I think it will be, I did not show the artboard resized.
The circle is unnecessary and is for demonstration only. To remove it, uncomment the last line.
// select item
var r = 18.8 * 2.83465;
var AB = 1 * 72;
var doc = app.activeDocument;
var x = doc.width / 2, y = -doc.height / 2;
var top = y + r / 2, left = x - r / 2;
var item = doc.selection[0];
item.position = [
(doc.width / 2) - (item.width / 2),
(-doc.height / 2) + (item.height / 2)
];
var circle = doc.pathItems.ellipse(top, left, r, r);
circle.move(item, ElementPlacement.PLACEAFTER);
...
Copy link to clipboard
Copied
I have a script that reduces proportionally according the width, maybe this can help but I have not figured out the other stuff. here it is hope it helps for somebody.
var doc = app.activeDocument;
var objeto = doc.selection[0];
var dup = objeto.duplicate();
var WidthInicial = objeto.width;
var WidthFinal = WidthInicial - 14.4;
var ScaleFactor = (WidthFinal / WidthInicial) * 100;
dup.resize(ScaleFactor, ScaleFactor, true, true, true, true, null, Transformation.TOP);
doc.selection = [];
dup.selected = true;
objeto.remove();
Copy link to clipboard
Copied
Got the following code so far, it makes your object 18.8mm wide or height, but still does not fit inside the circle of 18.8mm, you can give it a try.
var doc = app.activeDocument;
var objeto = doc.selection[0];
var anchoObjmm = (objeto.width / 72) * 25.4;
var altoObjmm = (objeto.height / 72) * 25.4;
if(anchoObjmm > altoObjmm){
alert("El objeto es mas ancho que alto");
var dup = objeto.duplicate();
var WidthInicial = anchoObjmm;
alert(anchoObjmm);
var diferencia = 18.8 - anchoObjmm;
alert(diferencia);
if(diferencia < 0){
var WidthFinal = WidthInicial + diferencia;
alert(WidthFinal);
}
else{
var WidthFinal = WidthInicial - diferencia;
alert(WidthFinal);
}
var ScaleFactor = (WidthFinal / WidthInicial) * 100;
alert(ScaleFactor);
dup.resize(ScaleFactor, ScaleFactor, true, true, true, true, null, Transformation.TOP);
doc.selection = [];
dup.selected = true;
objeto.remove();
}
else{
alert("El objeto es mas alto que ancho");
var dup = objeto.duplicate();
var altoInicial = altoObjmm;
var diferencia = 18.8 - altoObjmm;
if(diferencia < 0){
var altoFinal = altoInicial + diferencia;
}
else{
var altoFinal = altoInicial - diferencia;
}
var ScaleFactor = (altoFinal / altoInicial) * 100;
dup.resize(ScaleFactor, ScaleFactor, true, true, true, true, null, Transformation.TOP);
doc.selection = [];
dup.selected = true;
objeto.remove();
}
Copy link to clipboard
Copied
I just try trying to make a "resize" using a scale value from the area of the circle, which is pi * r2.
var doc = app.activeDocument;
var objeto = doc.selection[0];
var anchoObjmm = (objeto.width / 72) * 25.4;
var altoObjmm = (objeto.height / 72) * 25.4;
var areaObjmm2 = anchoObjmm * altoObjmm;
var ScaleFactor = (areaObjmm2 / 277.6) * 100;
var dup = objeto.duplicate();
dup.resize(ScaleFactor, ScaleFactor, true, true, true, true, null, Transformation.TOP);
doc.selection = [];
dup.selected = true;
objeto.remove();
still does nto fit inside the circle
Copy link to clipboard
Copied
The circle is unnecessary and is for demonstration only. To remove it, uncomment the last line.
// select item
var r = 18.8 * 2.83465;
var AB = 1 * 72;
var doc = app.activeDocument;
var x = doc.width / 2, y = -doc.height / 2;
var top = y + r / 2, left = x - r / 2;
var item = doc.selection[0];
item.position = [
(doc.width / 2) - (item.width / 2),
(-doc.height / 2) + (item.height / 2)
];
var circle = doc.pathItems.ellipse(top, left, r, r);
circle.move(item, ElementPlacement.PLACEAFTER);
circle.filled = false;
var m = item.height / item.width;
width = Math.sqrt(Math.pow(r / 2, 2) / (Math.pow(m, 2) + 1)) * 2;
height = width * m;
item.resize(100 * (width / item.width), 100 * (height / item.height));
doc.artboards[0].artboardRect = [x - AB / 2, y + AB / 2, x + AB / 2, y - AB / 2];
// circle.remove();
Copy link to clipboard
Copied
Since there is a lot of logos, with different sizes and shapes, this is the closest script to what I was looking for! it is not completely filling all the shape of the 18.8mm circle but the script is good! I hope mines were useful for somebody as well. thanks @femkeblanco
Copy link to clipboard
Copied
Hi community! Can I reopen this thread again or make a new one? I would like a modification, not sure how to do it.
the script from @femkeblanco :
// select item
var r = 19 * 2.83465;
var AB = 1 * 72;
var doc = app.activeDocument;
var x = doc.width / 2, y = -doc.height / 2;
var top = y + r / 2, left = x - r / 2;
var item = doc.selection[0];
item.position = [
(doc.width / 2) - (item.width / 2),
(-doc.height / 2) + (item.height / 2)
];
var circle = doc.pathItems.ellipse(top, left, r, r);
circle.move(item, ElementPlacement.PLACEAFTER);
circle.filled = false;
var m = item.height / item.width;
width = Math.sqrt(Math.pow(r / 2, 2) / (Math.pow(m, 2) + 1)) * 2;
height = width * m;
item.resize(100 * (width / item.width), 100 * (height / item.height));
doc.artboards[0].artboardRect = [x - AB / 2, y + AB / 2, x + AB / 2, y - AB / 2];
app.executeMenuCommand("fitin");
// circle.remove();
Now the circle needs to be 19mm instead of 18.8 and the artboard needs to be 22mm instead of 1in (25.4mm).
I was able to change 18.8mm to 19mm really easy but not sure how to reduce the artboard to 22mm. I would appreciate some help. Thanks!
Copy link to clipboard
Copied
You'll need to adjust the third line that calculates the artboard size. Illustrator uses points as the unit of measurement for almost all distances (1 inch equals 72 points). You can read more about that in the documentation here. So just convert 22 mm to points.
var AB = 22 * 2.834645;
Copy link to clipboard
Copied
I cant like the comments anymore but big like for you, thanks!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now