Skip to main content
Inspiring
November 14, 2022
Answered

Adjust spacing without adjusting other spacing

  • November 14, 2022
  • 2 replies
  • 3131 views

How can I, with a Script, automatically adjust only the spacing between the grouped squares where the space is less than .022" space between them?

 

Top Line is the Sample art.

Loop 1 - Focus on the spacing between Shape 1 and 2 is fine so no adjustment is needed.

Loop 2 - Focus on the spacing between Shape 2 and 3 is adjusted to 0.022"

Loop 3 - Focus on the spacing between Shape 3 and 4 is fine so no adjustment is needed.

Loop 4 - Focus on the spacing between Shape 4 and 5 is adjusted to 0.022".

 




Thank you!

This topic has been closed for replies.
Correct answer renél80416020

Does this do what you want? 

// instructions: select path items
var space = 0.022 * 72;  // minimum space between paths (inches)
var doc = app.activeDocument;
var paths = doc.pathItems;

// step 1: push selected paths into array to sort left to right
var array = [];
for (var i = 0; i < app.selection.length; i++) {
    array.push(app.selection[i]);
}
array.sort(function (a, b) {return a.left - b.left});

// step 2: move paths into group
var group = doc.groupItems.add();
for (var i = 0; i < array.length; i++) {
    // save path's name
    var tag1 = array[i].tags.add();
    tag1.name = "tag1";
    tag1.value = array[i].name;
    // rename path according to left to right order
    array[i].name = "x" + i;
    array[i].move(group, ElementPlacement.PLACEATBEGINNING);
}

// step 3: move paths out of group from left to right, & , 
// if distance between path-before-group & group < "space", 
// translate group distance of "space" from path-before-group
for (var i = group.pathItems.length - 1, j = 0; i > -1; i--, j++) {
    group.pathItems[i].move(group.parent, ElementPlacement.PLACEATEND);
    if (group.left - (paths["x" + j].left + paths["x" + j].width) < space) {
        group.left = paths["x" + j].left + paths["x" + j].width + space;
    }
    // rename path according to original name
    paths["x" + j].name = paths["x" + j].tags["tag1"].value;
}

Bonjour à tous,

femkeblanco, il me semble pour reprendre ton idée...

// instructions: select path items
var space = .022 * 72;  //  minimum space between paths (inches)

function alignObj0(tabs,space) {
  var esp;
  tabs.sort(function (a, b) {return a.left - b.left});
      for (var i = 1; i < tabs.length; i++) {
        esp = tabs[i].left-tabs[i-1].left-tabs[i-1].width;  // alert(esp);
          if (esp < space) {
              for (var j = i; j < tabs.length; j++) {
                tabs[j].left += space-esp;
              }
          }
      }
}
  if (selection.length) {alignObj0(selection,space);}

René

2 replies

pixxxelschubser
Community Expert
Community Expert
November 15, 2022

Do you really need a script for this?

Do you have a very large number of files in which you need to match spacing in this way? Or do you just want to know the script syntax?

 

After all, the existing drag-click-click function is fast and good. Or is it not?

 

 

drag = select the items

 

click = select the base object

 

insert the value (if not already done) and

click = align

 

 

Done.

Inspiring
November 15, 2022

I'm sorry, I don't think I'm explaining myself properly.  I'm not looking to have the shapes spaced equally distant. 

I need the spacing to remain exactly the same unless that space is less than .022".  So if the spacing between shapes is .075" , .015" , 1" .006" I need the spacing to change to  .075" , .022" , 1" .022".

 

I hope that clarifies things.

femkeblanco
Legend
November 15, 2022

Does this do what you want? 

// instructions: select path items
var space = 0.022 * 72;  // minimum space between paths (inches)
var doc = app.activeDocument;
var paths = doc.pathItems;

// step 1: push selected paths into array to sort left to right
var array = [];
for (var i = 0; i < app.selection.length; i++) {
    array.push(app.selection[i]);
}
array.sort(function (a, b) {return a.left - b.left});

// step 2: move paths into group
var group = doc.groupItems.add();
for (var i = 0; i < array.length; i++) {
    // save path's name
    var tag1 = array[i].tags.add();
    tag1.name = "tag1";
    tag1.value = array[i].name;
    // rename path according to left to right order
    array[i].name = "x" + i;
    array[i].move(group, ElementPlacement.PLACEATBEGINNING);
}

// step 3: move paths out of group from left to right, & , 
// if distance between path-before-group & group < "space", 
// translate group distance of "space" from path-before-group
for (var i = group.pathItems.length - 1, j = 0; i > -1; i--, j++) {
    group.pathItems[i].move(group.parent, ElementPlacement.PLACEATEND);
    if (group.left - (paths["x" + j].left + paths["x" + j].width) < space) {
        group.left = paths["x" + j].left + paths["x" + j].width + space;
    }
    // rename path according to original name
    paths["x" + j].name = paths["x" + j].tags["tag1"].value;
}
femkeblanco
Legend
November 14, 2022

Edit: The preceding code was unnecessarily complicated.

 

// instructions: select path items to space out
var space = 0.022 * 72;  // desired space between paths (inches)
var doc = app.activeDocument;
var paths = doc.pathItems;

// step 1: push the selected paths into an array to sort left to right
var array = [];
for (var i = 0; i < app.selection.length; i++) {
    array.push(app.selection[i]);
}
array.sort(function (a, b) {return a.left - b.left});

// step 2: translate the path the distance of "space" from the path before
for (var i = 1; i < array.length; i++) {
    array[i].left = array[i - 1].left + array[i - 1].width + space;
}