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

[Script] Resizing multiple objects propotrionally

Explorer ,
Oct 14, 2022 Oct 14, 2022

Hello everyone,
Here is my problem, I want to resize multiple objects with a script by selecting the size (in mm) that I want. I already have one script but this script don't resize proportionally. 

var centerObjects = false;
function main() {
    var item, center_point;
    var toPixels = function (v) {

        var units = {
            'in': 72,
            'mm': 2.8346455078125,
            'px': 1,
            'pt': 1,
        }, re = /(\d*[.]*\d+)(mm|in|ft|cm|px|pt)/i, m, u, rep;
        //derivitave
        units['cm'] = units['mm'] * 10;
        units['ft'] = units['in'] * 12;

        while (m = v.match(re)) {
            u = m[2].toLowerCase();
            if (units[u]) {
                rep = (m[1] * units[u]);
                v = v.replace(re, rep);
            }
        }
        return v;
    }

    var trace = function (m) { alert(m) };
    if (activeDocument == null) { trace("Pas de document ouvert trouvé. Veuillez s'il vous plaît ouvrir un document."); return; }
    if (activeDocument.selection.length == 0) { trace("Aucune sélection... 😞", "Veuillez sélectionner un pictogramme."); return; }
    var i = prompt("Quelles dimensions voulez-vous modifier ?", "width,height");
    if (i === null) { return false; }
    var v = prompt("Veuillez entrer les nouvelles dimensions du pictogramme", "5mm,5mm");
    v = toPixels(v);
    // here's where we walk through all objects.
    var assign = function (i, v) {
        for (var x = 0; x < activeDocument.selection.length; x++) {

            item = activeDocument.selection[x];
            //get top and left width and height values
            center_point = [item.top - (item.height / 2), item.left + (item.width / 2)];

            item[i] = eval(v);
            //redraw();
            if (centerObjects) {
                //center_point = [item.top+(item.height/2),item.left+(item.width/2)];
                item.top = center_point[0] + (item.height / 2);
                item.left = center_point[1] - (item.width / 2);
            }
        };
    }
    if (i.indexOf(',') !== -1) { i = i.split(','); }
    //split if a list, but not if function is found.
    if (v.indexOf(',') !== -1) { v = v.split(','); }

    if (typeof i !== "string") {
        for (var len = i.length, c = 0; c < len; c++) {
            assign(i[c], typeof v !== 'string' ? v[c] : v);
        }
    } else {
        assign(i, v);
    }
    redraw();
    return true;

}
main();
 
 
Before running the script:
polotrobo_0-1665754770739.png

 

After runing the script:

polotrobo_1-1665754786439.png

As you can see it don't resize them proportionally..

Thank you,
PM

 

 
TOPICS
Import and export , Scripting , Third party plugins , Tools
1.6K
Translate
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 1 Correct answer

Community Expert , Oct 14, 2022 Oct 14, 2022

Hi,

Try following

var centerObjects = false;
function main() {
    var item, center_point;
    var toPixels = function (v) {

        var units = {
            'in': 72,
            'mm': 2.8346455078125,
            'px': 1,
            'pt': 1,
        }, re = /(\d*[.]*\d+)(mm|in|ft|cm|px|pt)/i, m, u, rep;
        //derivitave
        units['cm'] = units['mm'] * 10;
        units['ft'] = units['in'] * 12;

        while (m = v.match(re)) {
            u = m[2].toLowerCase();
            if (un
...
Translate
Adobe
Community Expert ,
Oct 14, 2022 Oct 14, 2022

Hi,

Try following

var centerObjects = false;
function main() {
    var item, center_point;
    var toPixels = function (v) {

        var units = {
            'in': 72,
            'mm': 2.8346455078125,
            'px': 1,
            'pt': 1,
        }, re = /(\d*[.]*\d+)(mm|in|ft|cm|px|pt)/i, m, u, rep;
        //derivitave
        units['cm'] = units['mm'] * 10;
        units['ft'] = units['in'] * 12;

        while (m = v.match(re)) {
            u = m[2].toLowerCase();
            if (units[u]) {
                rep = (m[1] * units[u]);
                v = v.replace(re, rep);
            }
        }
        return v;
    }

    var trace = function (m) { alert(m) };
    if (activeDocument == null) { trace("Pas de document ouvert trouvé. Veuillez s'il vous plaît ouvrir un document."); return; }
    if (activeDocument.selection.length == 0) { trace("Aucune sélection... 😞", "Veuillez sélectionner un pictogramme."); return; }
    var i = prompt("Quelles dimensions voulez-vous modifier ?", "width,height");
    if (i === null) { return false; }
    var v = prompt("Veuillez entrer les nouvelles dimensions du pictogramme", "5mm,5mm");
    v = toPixels(v);
    v = v.split(',');
    for (var x = 0; x < activeDocument.selection.length; x++) {
        item = activeDocument.selection[x];
        var scaleX = v[0] / item.width;
        var scaleY = v[1] / item.height;
        var finalScaleFactor = scaleX > scaleY ? scaleY : scaleX;
        item.resize(finalScaleFactor * 100, finalScaleFactor * 100, true);
    }
}
main();

 

 

Best regards
Translate
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
Explorer ,
Oct 16, 2022 Oct 16, 2022

Hi @Charu Rajput 

 

Thank you so much it's perfect !!

 

Best regards

Translate
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 ,
Oct 16, 2022 Oct 16, 2022

You can also try my free "ResizeToSize" script to resize objects with different settings. Units are taken from the document settings. Uniform - proportional scaling. You can choose which side to calculate. Change the size by a delta. https://github.com/creold/illustrator-scripts/blob/master/md/Item.md#resizetosize

 

rts.gif

Translate
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
Explorer ,
Oct 16, 2022 Oct 16, 2022
LATEST

Thank you @Sergey Osokin it's a very useful script 🙂

 

Best regards

Translate
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