Skip to main content
Known Participant
October 14, 2022
Answered

[Script] Resizing multiple objects propotrionally

  • October 14, 2022
  • 2 replies
  • 1596 views

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:

 

After runing the script:

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

Thank you,
PM

 

 
Correct answer Charu Rajput

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();

 

 

2 replies

Sergey Osokin
Inspiring
October 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

 

polotroboAuthor
Known Participant
October 17, 2022

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

 

Best regards

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
October 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
polotroboAuthor
Known Participant
October 17, 2022

Hi @Charu Rajput 

 

Thank you so much it's perfect !!

 

Best regards