Skip to main content
Inspiring
December 5, 2022
Answered

Help Adjusting Space based on Shape Height

  • December 5, 2022
  • 2 replies
  • 1238 views

Hello,

I currently have a script that will adjust the spacing between shapes based on the size of the shape. 

 

If the shapes are smaller than 0.2" in height or larger than 0.5" in height, I want the shapes NOT to move,  but an alert to come up instead, referencing the size is out of Production Standards. 
Could anyone please take a look at what I have and help me?
Thank you!

 

 

// instructions: select path items
var space = .0235 * 72;  //  minimum space between paths (inches)
var doc = app.activeDocument;
var sel = doc.selection.height;
var maxHeight = 0;
// Capture Height
for (var i = 0; i < app.selection.length; i++) {
    var _item = app.selection[i];
    if (_item.height > maxHeight) {
        var maxHeight = _item.height;
    };
}
  var shpHeight = (maxHeight / 72)*.9525;
function alignObj0(tabs,space) {
  var esp;
  // Max Height If Statement Specs
    if (shpHeight <= 0.2){
        var maxOverlap = 0
        var nestOverlap = 0
        alert("Size is Outside of Production Standards");
    }if (shpHeight > 0.2 && shpHeight <= 0.25){
        var maxOverlap = -2.54
        var nestOverlap = -0.85
        // alert("Small");
    }if (shpHeight > 0.25 && shpHeight <= 0.35){
        var maxOverlap = -4.74
        var nestOverlap = -1.22
        //alert("Medium");
    }if (shpHeight > 0.35 && shpHeight <= 0.45){
        var maxOverlap = -6.42
        var nestOverlap = -1.82
        //alert("Large"); 
    }if (shpHeight > 0.45 && shpHeight <= 0.5){
        var maxOverlap = -8.16
        var nestOverlap = -2.47
        //alert("X Large"); 
    }if (shpHeight > 0.5){
        var maxOverlap = 0
        var nestOverlap = 0
        alert("Size is Outside of Production Standards");
    };
        tabs.sort(function (a, b) {return a.left - b.left});
      for (var i = 1; i < tabs.length; i++) {
        // Really Tight Spacing
        esp = tabs[i].left-tabs[i-1].left-tabs[i-1].width;   //alert(esp);
          if (esp < maxOverlap) {
              for (var j = i; j < tabs.length; j++) {
                tabs[j].left += (esp*(-1))+space;
              }
        // Natural Nested Spacing
          } if (esp > maxOverlap && esp <= nestOverlap) {
              for (var j = i; j < tabs.length; j++) {
                tabs[j].left += 0
              }
        // Needs Spacing Situation 1
          } if (esp > nestOverlap && esp <= 0) {
              for (var j = i; j < tabs.length; j++) {   
                tabs[j].left += space-((esp/5)*4)
              }
        // Needs Spacing Situation 2
          } if (esp > 0 && esp <= space) {
              for (var j = i; j < tabs.length; j++) {   
                tabs[j].left += space-((esp/5)*4)
              }
        // Spacing is Good as is
          } if (esp > space) {
              for (var j = i; j < tabs.length; j++) {   
                tabs[j].left += (space * 0)
              }
          }
      }
};
  if (selection.length) {alignObj0(selection,space);};

 

 

Thank you!

This topic has been closed for replies.
Correct answer m1b

Thanks. Your code works on your test file except that even if the size is "Outside of production standards" it still does the spacing. This is fixed by adding a "return" after each alert.

alert("Size is Outside of Production Standards");
return;

Make sense?

- Mark 

2 replies

Inspiring
December 5, 2022
test_file.pdf
m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
December 5, 2022

Thanks. Your code works on your test file except that even if the size is "Outside of production standards" it still does the spacing. This is fixed by adding a "return" after each alert.

alert("Size is Outside of Production Standards");
return;

Make sense?

- Mark 

Known Participant
December 6, 2022

Is there a way to completely stop the execution of the script?

m1b
Community Expert
Community Expert
December 5, 2022

Hi @BryanPagenkopf, it would be really helpful to post a sample document including examples items that are good sizes and bad sizes, and post screenshot showing which are good and which are bad. That way, our expectations are the same.

- Mark