Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Created June 6, 2012 21:53
Show Gist options
  • Save hilukasz/2885072 to your computer and use it in GitHub Desktop.
Save hilukasz/2885072 to your computer and use it in GitHub Desktop.
//start script
var subLayers = app.activeDocument.layers.getByName("parent").pageItems;
var smallestParentArea = 100000000;
var parentSmallest = [];
var smallestChild = "no smallest child found yet";
//loop through all sublayers to find children
for(var i = 0; i < subLayers.length; i++){
var smallestChild;
var 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 -----");
if(isIn(childContainer, parentContainer) && childContainerArea < parentArea ) {
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;
parentSmallest.push(parentContainer);
print("parent is:"+parentContainer.name+"\n");
}
}
}
}
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:
$.writeln("!!! "+smallestChild.name+" is the smallest conatiner. it's position is: "+smallestChild.visibleBounds+"\n \n"+"its parent(s) is/are: "+parentSmallest);
//clear variables because ES is stupid sometimes
var childContainer = null,
childContainerArea = null,
parentArea = null,
parentContainer = null,
isSame = null;
for(var i = 0; i < subLayers.length; i++){
var smallestChild;
var 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 -----");
if(isIn(childContainer, parentContainer) && childContainerArea < parentArea ) {
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;
parentSmallest.push(parentContainer);
print("parent is:"+parentContainer.name+"\n");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment