Guest User

Untitled

a guest
Oct 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. var subLayers = app.activeDocument.layers.getByName("parent").pageItems;
  2.  
  3. var smallestParentArea = 10000000;
  4.  
  5. var smallestChild = "this is the first smallest child found";
  6. var parentSmallest = [];
  7. //loop through all sublayers to find children
  8. for(var i = 0; i < subLayers.length; i++){
  9. var smallestChild,
  10. childContainer = subLayers[i],
  11. childContainerArea = childContainer.width * childContainer.height
  12.  
  13. print("#################"+childContainer.name+" : CHILD compare ###################");
  14.  
  15. //compare all subLayers to other sublayers
  16. for(var j = 0; j < subLayers.length; j++){
  17. var parentContainer = subLayers[j],
  18. parentArea = parentContainer.width * parentContainer.height;
  19. // check to see if it is comparing to itself, skip if it is
  20. if (i != j){
  21. // check to see if childContainer is in another layer
  22. print("-----"+parentContainer.name+" : PARENT compair -----");
  23. var usedContainerIndexes = [];
  24. if(isIn(childContainer, parentContainer) && childContainerArea < parentArea && usedContainerIndexes.indexOf(j) === -1) {
  25. //if(smallestChild =="string"){ smallestChild.name };
  26. print(">> TEST :: "+childContainer.name+": SMALLEST CHILD // area:"+childContainerArea+" // "+smallestChild+" (index: "+j+") USED TO BE SMALLEST CHILD");
  27. smallestChild = childContainer;
  28. smallestParentArea = parentContainerArea;
  29. usedContainerIndexes.push(j);
  30. parentSmallest.push(parentContainer);
  31. print("parent is:"+parentContainer.name+"\n");
  32. }
  33. }
  34. }
  35. }
  36.  
  37. function findSmallestObject() {
  38. }
  39.  
  40.  
  41. function isIn(child, parentItem){
  42. var childVB = child.visibleBounds,
  43. childLeft = childVB[0],
  44. childTop = Math.abs(childVB[1]),
  45. childRight = childVB[2],
  46. childBottom = Math.abs(childVB[3]);
  47. var parentVB = parentItem.visibleBounds,
  48. parentLeft = parentVB[0],
  49. parentTop = Math.abs(parentVB[1]),
  50. parentRight = parentVB[2],
  51. parentBottom = Math.abs(parentVB[3]);
  52.  
  53. $.write(child.name+" : [ left: "+childVB[0]+" , top: "+Math.abs(childVB[1]) +" , right: "+childVB[2] +", bottom: "+Math.abs(childVB[3])+"] \n\n" );
  54. $.write(parentItem.name+" : [ left: "+parentVB[0]+", top: "+Math.abs(parentVB[1]) +" , right: "+parentVB[2] +" , bottom: "+Math.abs(parentVB[3])+"] \n\n" );
  55. //return true if child is in parent
  56. return childLeft >= parentLeft &&
  57. childTop >= parentTop &&
  58. childRight <= parentRight &&
  59. childBottom <= parentBottom;
  60. }
  61.  
  62. //check what the smallest container is:
  63.  
  64. for(var i = 0; i < parentSmallest.length; i++){
  65. var smallestParent;
  66. var parent = parentSmallest[i];
  67. var parentArea = parentSmallest[i].height*parentSmallest[i].width;
  68. $.writeln("\n\n"+parentSmallest[i].name+" %% PARENT NAME %%, area: "+parentArea);
  69. for(var j = 0; j < parentSmallest.length; j++){
  70. if (i != j){ //don't check against itself
  71. var child = parentSmallest[j];
  72. var childArea = parentSmallest[j].height*parentSmallest[j].width;
  73. $.writeln(parentSmallest[j].name+" %% CHILD NAME %%, area: "+childArea);
  74. if(childArea < parentArea){
  75. $.writeln(">>>>"+parentSmallest[j].name+" is now the smallest");
  76. smallestParent = child;
  77. }
  78. }
  79. }
  80. }
  81. var arr = {};
  82.  
  83. for ( i=0; i < things.thing.length; i++ )
  84. arr[things.thing[i]['id']] = things.thing[i];
  85.  
  86. things.thing = new Array();
  87. for ( key in arr )
  88. things.thing.push(arr[key]);
  89.  
  90.  
  91. $.writeln("\n\n"+layersArray+" new array");
  92.  
  93. $.writeln("!!! "+smallestChild.name+" is the smallest conatiner. it's position is: "+smallestChild.visibleBounds+"\n \n"+"its parent(s) is/are: "+parentSmallest);
  94. $.write("\n\n smallestparent:"+smallestParent);
  95.  
  96. $.writeln("\n\n"+layersArray+" new array");
  97. //clear variables because ES is stupid sometimes
  98. var childContainer = null,
  99. childContainerArea = null,
  100. parentArea = null,
  101. parentContainer = null,
  102. isSame = null;
Add Comment
Please, Sign In to add comment