Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Created June 7, 2012 03:52
Show Gist options
  • Save hilukasz/2886466 to your computer and use it in GitHub Desktop.
Save hilukasz/2886466 to your computer and use it in GitHub Desktop.
var subLayers = app.activeDocument.layers.getByName("parent").pageItems;
var smallestParentArea = 10000000;
var smallestChild = "this is the first smallest child found";
var parentSmallest = [];
//loop through all sublayers to find children
for(var i = 0; i < subLayers.length; i++){
var smallestChild,
childContainer = subLayers[i],
childContainerArea = childContainer.width * childContainer.height
print("#################"+childContainer.name+" : CHILD compare ###################");
//compare all subLayers to other sublayers
for(var j = 0; j < subLayers.length; j++){
var parentContainer = subLayers[j],
parentArea = parentContainer.width * parentContainer.height;
// check to see if it is comparing to itself, skip if it is
if (i != j){
// check to see if childContainer is in another layer
print("-----"+parentContainer.name+" : PARENT compair -----");
var usedContainerIndexes = [];
if(isIn(childContainer, parentContainer) && childContainerArea < parentArea && usedContainerIndexes.indexOf(j) === -1) {
//if(smallestChild =="string"){ smallestChild.name };
print(">> TEST :: "+childContainer.name+": SMALLEST CHILD // area:"+childContainerArea+" // "+smallestChild+" (index: "+j+") USED TO BE SMALLEST CHILD");
smallestChild = childContainer;
smallestParentArea = parentContainerArea;
usedContainerIndexes.push(j);
parentSmallest.push(parentContainer);
print("parent is:"+parentContainer.name+"\n");
}
}
}
}
function findSmallestObject() {
}
function isIn(child, parentItem){
var childVB = child.visibleBounds,
childLeft = childVB[0],
childTop = Math.abs(childVB[1]),
childRight = childVB[2],
childBottom = Math.abs(childVB[3]);
var parentVB = parentItem.visibleBounds,
parentLeft = parentVB[0],
parentTop = Math.abs(parentVB[1]),
parentRight = parentVB[2],
parentBottom = Math.abs(parentVB[3]);
$.write(child.name+" : [ left: "+childVB[0]+" , top: "+Math.abs(childVB[1]) +" , right: "+childVB[2] +", bottom: "+Math.abs(childVB[3])+"] \n\n" );
$.write(parentItem.name+" : [ left: "+parentVB[0]+", top: "+Math.abs(parentVB[1]) +" , right: "+parentVB[2] +" , bottom: "+Math.abs(parentVB[3])+"] \n\n" );
//return true if child is in parent
return childLeft >= parentLeft &&
childTop >= parentTop &&
childRight <= parentRight &&
childBottom <= parentBottom;
}
//check what the smallest container is:
for(var i = 0; i < parentSmallest.length; i++){
var smallestParent;
var parent = parentSmallest[i];
var parentArea = parentSmallest[i].height*parentSmallest[i].width;
$.writeln("\n\n"+parentSmallest[i].name+" %% PARENT NAME %%, area: "+parentArea);
for(var j = 0; j < parentSmallest.length; j++){
if (i != j){ //don't check against itself
var child = parentSmallest[j];
var childArea = parentSmallest[j].height*parentSmallest[j].width;
$.writeln(parentSmallest[j].name+" %% CHILD NAME %%, area: "+childArea);
if(childArea < parentArea){
$.writeln(">>>>"+parentSmallest[j].name+" is now the smallest");
smallestParent = child;
}
}
}
}
var arr = {};
for ( i=0; i < things.thing.length; i++ )
arr[things.thing[i]['id']] = things.thing[i];
things.thing = new Array();
for ( key in arr )
things.thing.push(arr[key]);
$.writeln("\n\n"+layersArray+" new array");
$.writeln("!!! "+smallestChild.name+" is the smallest conatiner. it's position is: "+smallestChild.visibleBounds+"\n \n"+"its parent(s) is/are: "+parentSmallest);
$.write("\n\n smallestparent:"+smallestParent);
$.writeln("\n\n"+layersArray+" new array");
//clear variables because ES is stupid sometimes
var childContainer = null,
childContainerArea = null,
parentArea = null,
parentContainer = null,
isSame = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment