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

Help Adjusting Space based on Shape Height

Engaged ,
Dec 05, 2022 Dec 05, 2022

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

537

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 Expert ,
Dec 05, 2022 Dec 05, 2022

Copy link to clipboard

Copied

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

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
Engaged ,
Dec 05, 2022 Dec 05, 2022

Copy link to clipboard

Copied

 

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 Expert ,
Dec 05, 2022 Dec 05, 2022

Copy link to clipboard

Copied

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 

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 ,
Dec 05, 2022 Dec 05, 2022

Copy link to clipboard

Copied

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

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 Expert ,
Dec 05, 2022 Dec 05, 2022

Copy link to clipboard

Copied

LATEST

@Dmitriy27445167miri in the case of this script, the return I added will stop the script because the call to alignObj0 is the last line of the script; returning from it will do nothing and the script will end. Basically if you want the script to end, you need to structure it so it will. A simple approach is to put the whole script in a single function, then any time you return from it, the script ends. - Mark

 

Edit: see my answer here for example of wrapping the whole script in a function.

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