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

Bulk aligning and sorting a selection of objects?

Community Beginner ,
Jan 16, 2023 Jan 16, 2023

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

TOPICS
Scripting

Views

631

Translate

Translate

Report

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

Guide , Jan 16, 2023 Jan 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.s
...

Votes

Translate

Translate
Adobe
Guide ,
Jan 16, 2023 Jan 16, 2023

Copy link to clipboard

Copied

Can you show screenshots of a before and after?  

Votes

Translate

Translate

Report

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
Community Beginner ,
Jan 16, 2023 Jan 16, 2023

Copy link to clipboard

Copied

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

 

Screenshot 2023-01-16 at 14.01.08.png

Votes

Translate

Translate

Report

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
Guide ,
Jan 16, 2023 Jan 16, 2023

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]];
    }
}

Votes

Translate

Translate

Report

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
Community Beginner ,
Jan 17, 2023 Jan 17, 2023

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?

Votes

Translate

Translate

Report

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
Guide ,
Jan 17, 2023 Jan 17, 2023

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]
            ];
        }
    }
}

 

Votes

Translate

Translate

Report

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
Community Beginner ,
Jan 18, 2023 Jan 18, 2023

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!

Votes

Translate

Translate

Report

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
Guide ,
Jan 18, 2023 Jan 18, 2023

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;
        }
    }
}

 

 

Votes

Translate

Translate

Report

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
Community Beginner ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

Thanks again, this works great with the sets.

It seems to be when there is a variance in the length of the word in the list, I've just tried with a matching word and it works great.

I've attached an image of the output with varying word length.

Votes

Translate

Translate

Report

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
Community Beginner ,
Jan 27, 2023 Jan 27, 2023

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

Votes

Translate

Translate

Report

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
Guide ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

Sorry.  I don't know what the problem you are having is.  Could you expound, preferably with screenshots? 

Votes

Translate

Translate

Report

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
Community Beginner ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

Thanks for taking a look for me. 
I've realised that it's only happened when the amount of objects in a row is anything other than 7, in these cases the objects are not aligned or spaced out correctly. I've attached a screenshot to show how it looks with the number of rows set to 8.

Votes

Translate

Translate

Report

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
Guide ,
Jan 30, 2023 Jan 30, 2023

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. 

Votes

Translate

Translate

Report

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
Community Beginner ,
Jan 31, 2023 Jan 31, 2023

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

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