Skip to main content
zBob
Known Participant
June 2, 2023
Answered

distributing all elements evenly

  • June 2, 2023
  • 3 replies
  • 886 views

Hello everyone,
I am looking for a method or script to distribute many different elements/symbols evenly on a grid. What I mean is how to scale the length and height without scaling the objects themselves. Thanks for your tips!

 

 

This topic has been closed for replies.
Correct answer femkeblanco

This is a quick script implementation of @Jacob Bugge's idea. 

// select relevant page items
var doc = app.activeDocument;

var array1 = [];
for (var i = 0; i < doc.selection.length; i++) {
    array1.push(doc.selection[i]);
}
array1.sort(function (a, b) {
    return a.left - b.left;
});
var bounds1 = array1[0].geometricBounds;
var x1 = bounds1[0] + ((bounds1[2] - bounds1[0]) / 2);

var array2 = [];
for (var i = 1; i < array1.length; i++) {
    var bounds2 = array1[i].geometricBounds;
    var x2 = bounds2[1] + ((bounds2[3] - bounds2[1]) / 2);
    if (x2 > bounds1[3] && x2 < bounds1[1]) {
        array2.push(array1[i]);
    }
}
array2.sort(function (a, b) {
    return a.left - b.left;
});
var bounds2 = array2[0].geometricBounds;
var x2 = bounds2[0] + ((bounds2[2] - bounds2[0]) / 2);

var x = x2 - x1;
var y = Number(prompt("", "", "y (points)"));

var group = doc.groupItems.add();
for (var i = 0; i < doc.selection.length; i++) {
   doc.selection[i].moveToEnd(group);
}
group.resize(100 * y/x, 100 * y/x);

for (var i = group.pageItems.length - 1; i > -1 ; i--) {
    group.pageItems[i].resize(100 * x/y, 100 * x/y);
    group.pageItems[i].moveAfter(group);
}

 

3 replies

femkeblanco
femkeblancoCorrect answer
Legend
June 2, 2023

This is a quick script implementation of @Jacob Bugge's idea. 

// select relevant page items
var doc = app.activeDocument;

var array1 = [];
for (var i = 0; i < doc.selection.length; i++) {
    array1.push(doc.selection[i]);
}
array1.sort(function (a, b) {
    return a.left - b.left;
});
var bounds1 = array1[0].geometricBounds;
var x1 = bounds1[0] + ((bounds1[2] - bounds1[0]) / 2);

var array2 = [];
for (var i = 1; i < array1.length; i++) {
    var bounds2 = array1[i].geometricBounds;
    var x2 = bounds2[1] + ((bounds2[3] - bounds2[1]) / 2);
    if (x2 > bounds1[3] && x2 < bounds1[1]) {
        array2.push(array1[i]);
    }
}
array2.sort(function (a, b) {
    return a.left - b.left;
});
var bounds2 = array2[0].geometricBounds;
var x2 = bounds2[0] + ((bounds2[2] - bounds2[0]) / 2);

var x = x2 - x1;
var y = Number(prompt("", "", "y (points)"));

var group = doc.groupItems.add();
for (var i = 0; i < doc.selection.length; i++) {
   doc.selection[i].moveToEnd(group);
}
group.resize(100 * y/x, 100 * y/x);

for (var i = group.pageItems.length - 1; i > -1 ; i--) {
    group.pageItems[i].resize(100 * x/y, 100 * x/y);
    group.pageItems[i].moveAfter(group);
}

 

Jacob Bugge
Community Expert
Community Expert
June 2, 2023

Excellent, Femke.

 

Jacob Bugge
Community Expert
Community Expert
June 2, 2023

Bob,

 

Remembering and rereading your OP (Original Post) with this, "I am looking for a method or script", I am convinced that it would be easy to create a script that performs those tranformations; for a scripter that is.

 

Jacob Bugge
Community Expert
Community Expert
June 2, 2023

Bob,

 

You can scale them up by y/x, then scale each down again with Object>Transform>Transform Each, using the reverse scaling factor x/y.

 

Specifically, you can select all of them, then:

 

1) Start with the Transform panel by multiplying the W or H value by y/x, or the with Object>Transform>Scale by inserting 100y/x as Uniform (100 because it works in %),

2) End with the Object>Transform>Transform Each by inserting 100x/y in both directions (100 because it also works in %).

 

zBob
zBobAuthor
Known Participant
June 2, 2023

Hi Jacob,

Thanks for your tip - it works, indeed; I have used this trick before. However, it's only sometimes useful, especially not when I got many elements in many different compositions to change - it just takes a lot of time, especially calculating the percentual ratio. I want to use something like Transform Each, with the option to skip the scaling vector itself.

Best

Jacob Bugge
Community Expert
Community Expert
June 2, 2023

You are welcome, Bob.

 

"calculating the percentual ratio"

 

You can leave the calculation to Illy (job description Adobe Illustrator), by inserting the quotient itself, instead of calculating it.

 

As I mentioned, if you wish to go from x to y, you just write 100y/x, in other words writing 100 times y followed by / and followed by x, just adding 00 after y if it is a whole number, otherwise moving the decimal point/comma two places to the right (and add 0 if y only has one decimal place.

 

To show some samples with the changes to y shown bold red underlined, this means that if you wish to:

 

Scale up from x = 13 to y = 36, you just write 3600/13

 

Scale up from x = 13 to y = 36.723, you just write 3672.3/13

 

Scale up from x = 13 to y = 36.7, you just write 3670/13

 

 

And the opposite for Transform Each, just swapping x and y.