Skip to main content
Known Participant
January 16, 2023
Answered

Bulk aligning and sorting a selection of objects?

  • January 16, 2023
  • 1 reply
  • 1633 views

I'm looking to left align a selection of objects which have been scaled as part of a script then distribute the spacing at a set amount of 14.1732p

 

Is there a way to rearrange the selection into columns of objects if over a certain amount?

eg. rows of 7?

 

Below is the code from another thread adjust to individually re-size the objects proportionally if over a max dimension.

 

function scaleDownProportionally(item, maxSize) {
var W = item.width,
H = item.height,
MW = maxSize.W,
MH = maxSize.H,
factor = W / H > MW / MH ? MW / W * 100 : MH / H * 100;

if (W > MW || H > MH) {
item.resize(factor, factor);
}
}

for (var i = 0; i < app.selection.length; i++) {
scaleDownProportionally(app.selection[i], {W: 853.228, H: 127.559});
}

This topic has been closed for replies.
Correct answer femkeblanco

To resize about the left, change

item.resize(factor, factor);

in your script to

item.resize(factor, factor, undefined, undefined, undefined, undefined, undefined, Transformation.LEFT);

 

To rearrange the column into columns each with seven rows, add to the end of your script

rearrange(5 /*gap between columns (mm)*/,
          7 /*number of rows in column*/);
function rearrange(gap, n) {
    var a = [];
    for (var i = 0; i < app.selection.length; i++) {
        a.push(app.selection[i]);
    }
    a.sort(function(a, b) {return b.width - a.width;});
    var w = a[0].width;
    a.sort(function(a, b) {return b.top - a.top;});
    for (var i = n; i < a.length; i++) {
        a[i].position = [a[i - n].position[0] + w + gap * 2.835, 
                         a[i - n].position[1]];
    }
}

1 reply

femkeblanco
Legend
January 16, 2023

Can you show screenshots of a before and after?  

Known Participant
January 16, 2023

Thanks for the reply, I've attached a screenshot that hopefully helps

 

femkeblanco
femkeblancoCorrect answer
Legend
January 16, 2023

To resize about the left, change

item.resize(factor, factor);

in your script to

item.resize(factor, factor, undefined, undefined, undefined, undefined, undefined, Transformation.LEFT);

 

To rearrange the column into columns each with seven rows, add to the end of your script

rearrange(5 /*gap between columns (mm)*/,
          7 /*number of rows in column*/);
function rearrange(gap, n) {
    var a = [];
    for (var i = 0; i < app.selection.length; i++) {
        a.push(app.selection[i]);
    }
    a.sort(function(a, b) {return b.width - a.width;});
    var w = a[0].width;
    a.sort(function(a, b) {return b.top - a.top;});
    for (var i = n; i < a.length; i++) {
        a[i].position = [a[i - n].position[0] + w + gap * 2.835, 
                         a[i - n].position[1]];
    }
}