Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Created June 6, 2012 16:06
Show Gist options
  • Save hilukasz/2882949 to your computer and use it in GitHub Desktop.
Save hilukasz/2882949 to your computer and use it in GitHub Desktop.
//start library
function MyLayer(input) {
this.doc = app.activeDocument;
this.myLayerIndex = input || 0;
this.myLayerItem = this.hasLayerIndex() ? this.doc.layers[this.myLayerIndex] : false;
this.subLayers = [];
//this.visibleBounds = this.myLayerItem.visibleBounds;
// push pageItems to SubLayer constructor/class
for (var i = 0; i < this.myLayerItem.pageItems.length; i++) {
var pageItem = this.myLayerItem.pageItems[i];
//var visibleBounds = this.myLayerItem[i].visibleBounds;
this.subLayers.push(new SubLayer(pageItem));
}
this.width = this.myLayerItem.width;
this.height = this.myLayerItem.height;
};
MyLayer.prototype.hasLayerIndex = function() {
return this.myLayerIndex <= this.doc.layers.length - 1;
};
function SubLayer(input) {
return this.pageItem = input;
return this.visibleBounds = this.visibileBounds;
//this.top = this.visibleBounds.top;
}
SubLayer.prototype.isIn = function(parentItem) {
var parentVB = parentItem.visibleBounds;
$.write(this.name+" : "+this.pageItem.left+" left, "+this.pageItem.top+"top, "+this.pageItem.right+"right, "+this.pageItem.bottom+", bottom \n ");
$.write(this.pageItem.name+" : "+parentVB[0]+" left, "+parentVB[1] +"top, "+parentVB[2] +"right, "+parentVB[3]+", bottom \n" );
return this.pageItem.left >= parentVB[0] &&
this.pageItem.top >= parentVB[1] &&
this.pageItem.right <= parentVB[2] &&
this.pageItem.bottom >= parentVB[3];
};
*/
#target illustrator
//start script
var subLayers = app.activeDocument.layers.getByName("parent").pageItems;
var smallestArea = 100000000;
for(var i = 0; i < subLayers.length; i++){
var smallestContainer;
var currentLayer = subLayers[i],
currentLayerArea = currentLayer.width * currentLayer.height;
//print(currentLayer.name+" : "+" i: "+i+"\np height: "+currentLayer.height+"\np width: "+currentLayer.width+"\np VB:"+currentLayer.visibleBounds);
for(var j = 0; j < subLayers.length; j++){
// check to see if it is comparing to itself, skip if it is
if (i != j){
var compareLayer = subLayers[j],
compareArea = compareLayer.width * compareLayer.height;
//print(" C "+compareLayer.name+" : "+" j: "+j+" \n c height: "+compareLayer.height+"\n c width: "+compareLayer.width+"\n c VB:"+compareLayer.visibleBounds);
// check to see if currentLayer is in compare layer
if(isIn(currentLayer, compareLayer) && currentLayerArea < smallestArea ) {
print("TEST :: "+currentLayer.name+" is smallest layer and its area is: "+currentLayerArea+" // "+smallestContainer.name+" used to be smallest area");
smallestContainer = currentLayer;
smallestArea = currentLayerArea;
}
}else { $.writeln(subLayers[j].name+" = "+subLayers[i].name+" compare skipped"); }
}
print("################# end of compare ###################");
}
function isIn(child, parentItem){
var parentVB = parentItem.visibleBounds,
parentLeft = parentVB[0],
parentTop = Math.abs(parentVB[1]),
parentRight = parentVB[2],
parentBottom = Math.abs(parentVB[3]);
var childVB = child.visibleBounds,
childLeft = childVB[0],
childTop = Math.abs(childVB[1]),
childRight = childVB[2],
childBottom = Math.abs(childVB[3]);
$.write(child.name+" : \n [ left: "+childVB[0]+" , top: "+Math.abs(childVB[1]) +" , right: "+childVB[2] +", bottom: "+Math.abs(childVB[3])+"] \n\n" );
$.write(parentItem.name+" : \n [ 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("!!! "+smallestContainer.name+" is the smallest conatiner. it's position is: "+smallestContainer.visibleBounds+"\n \n");
//clear variables because ES is stupid sometimes
var currentLayer = null,
currentLayerArea = null,
compareArea = null,
compareLayer = null,
isSame = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment