Copy link to clipboard
Copied
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});
}
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.s
...
Copy link to clipboard
Copied
Can you show screenshots of a before and after?
Copy link to clipboard
Copied
Thanks for the reply, I've attached a screenshot that hopefully helps
Copy link to clipboard
Copied
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]];
}
}
Copy link to clipboard
Copied
This works perfectly, thank you so much.
Is there a way to organise distribute the spacing between the items in each column by a set amount as well?
Copy link to clipboard
Copied
Replace the preceding function with this
rearrange(10 /*gap between rows (mm)*/,
5 /*gap between columns (mm)*/,
7 /*number of rows in column*/);
function rearrange(gap1, gap2, 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 = 1; i < a.length; i++) {
if (i < n) {
a[i].position = [
a[i].position[0],
a[i - 1].position[1] - a[i - 1].height - gap1 * 2.835
];
} else {
a[i].position = [
a[i - n].position[0] + w + gap2 * 2.835,
a[i - n].position[1]
];
}
}
}
Copy link to clipboard
Copied
Thank you very much femkeblanco.
This works but is currently only applying the spacing to the first column and not subsequent columns?
Is it also possible to have a max number of columns before starting a new batch? I realised that with long lists the process takes it off the workspace. For example 6 columns before starting a new set underneath with a wider gap.
Alternatively I could set the amount of rows higher and split manually afterwards.
Thanks again!
Copy link to clipboard
Copied
The above function works as expected for me (shown below). What are the circumstances in which it is not working for you? The function is modified below to rearrange the original column into sets. In the example below, a set is 3 columns.
rearrange(10 /*gap between rows (mm)*/,
5 /*gap between columns (mm)*/,
7 /*number of rows in column*/,
3 /*number of columns per set*/);
function rearrange(gap1, gap2, n, m) {
var start = counter = 0;
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 = 0; i < a.length; i++) {
if (i == start) {
if (i == 0) {
a[i].position = [
a[0].position[0],
a[0].position[1]
];
} else {
a[i].position = [
a[0].position[0],
a[i - 1].position[1] - a[i - 1].height - gap1 * 2.835 * 2
// gap between sets twice gap between rows
];
}
} else if (i < start + n) {
a[i].position = [
a[0].position[0],
a[i - 1].position[1] - a[i - 1].height - gap1 * 2.835
];
} else {
a[i].position = [
a[i - n].position[0] + w + gap2 * 2.835,
a[i - n].position[1]
];
}
counter++;
if (counter == n * m) {
start += counter;
counter = 0;
}
}
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
With my attempts I haven't been able to make sure the spacing is arranged post sorting into columns and sets?
Any ideas with this? @femkeblanco
Thanks again for all the help
Copy link to clipboard
Copied
Sorry. I don't know what the problem you are having is. Could you expound, preferably with screenshots?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Unfortunately, I think we have reached a limitation of the above script. This arranges the first column first, so it doesn't account for variable heights in the succeeding columns. I can arrange the first column based on a max height, but the result will not look tidy. (This is compounded by the fact that the arranging is based on the text frame top position, not base line.) You could try (a) increasing the first argument (gap between rows) until you get an acceptable result (with no overlap) or (b) starting over by posting a new question.
Copy link to clipboard
Copied
Thanks for the response! I think in most cases 7 will work perfectly and it's easy enough to align in other situation.
Again, thanks for all your help with this.