Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Created June 1, 2012 17:50
Show Gist options
  • Save hilukasz/2853975 to your computer and use it in GitHub Desktop.
Save hilukasz/2853975 to your computer and use it in GitHub Desktop.
////////////////////////////////
//
// mySymbol class
//
////////////////////////////////
function rotate90(input){ input.unshift(input.pop()); } //shift array one to right so it follows CSS style borders
//accepts layer name or index
function MySymbol(input){
this.doc = app.activeDocument;
this.mySymbolIndex = input || 0;
this.mySymbolItem = this.hasSymbolIndex() ? this.doc.symbolItems[this.mySymbolIndex] : false;
this.visibleBounds = this.mySymbolItem.visibleBounds;
this.controlBounds = this.mySymbolItem.controlBounds;
// array of 4 numbers for position of bounding box
this.name = this.mySymbolItem.symbol.name;
this.top = Math.abs(this.mySymbolItem.top);
this.left = this.mySymbolItem.left;
this.topLeft = [Math.abs(this.mySymbolItem.top), this.mySymbolItem.left];
}
MySymbol.prototype.getPosition = function(top, left) {
//if(topLeft) {
var relativePositionTop = this.top - top;
var relativePositionLeft= this.left - left;
return [relativePositionTop, relativePositionLeft];
//} else { return [this.top, this.left]; }
};
MySymbol.prototype.hasSymbolIndex = function() {
return this.mySymbolIndex <= this.doc.symbolItems.length - 1;
};
MySymbol.prototype.getWidth = function() {
return this.mySymbolItem ? Math.round(this.mySymbolItem.width) : 0;
};
MySymbol.prototype.getHeight = function() {
return this.mySymbolItem ? Math.round(this.mySymbolItem.height) : 0;
};
// returns width and height as an array
MySymbol.prototype.getWidthHeight = function() {
return [this.getWidth(), this.getHeight()];
}
MySymbol.prototype.isIn = function(parentItem) {
var isTrue,
child = this.visibleBounds,
parentVB = parentItem.visibleBounds,
parentLeft = parentVB[0], parentTop = Math.abs(parentVB[1]), parentRight = parentVB[2], parentBottom = Math.abs(parentVB[3]);
pointIsIn(parentLeft, parentTop, parentBottom, parentRight, child);
function pointIsIn(parentLeft, parentTop, parentBottom, parentRight, child) {
if ( child[0] >= parentLeft && Math.abs(child[1]) >= parentTop && child[2] <= parentRight && Math.abs(child[3]) <= parentBottom) {
//print("pass");
isTrue = true;
//return true;
} else {
//print("fail");
isTrue = false;
//return false;
}
}
//print("is true? "+isTrue);
if (isTrue){ return true; }
}
// get symbol by name
MySymbol.prototype.getByName = function(nameString) {
for (i=0; i<idoc.symbolItems.length; i++) // loop thru all symbol items in the active document
{
var symbolitem = idoc.symbolItems[i]; // get each symbol item
if (symbolitem.symbol.name == nameString) { // check if it is an instance of "Apples" symbol
symbolitem.selected = true; // if it is, then select it
}
}
}
//////////////////////////
//write CSV
//////////////////////////
function CSVFile(fileName) {
this.fileName = fileName;
}
CSVFile.prototype.getFilePath = function() {
var inputFolder = Folder.selectDialog();
return inputFolder + "/" + this.fileName + ".csv";
};
CSVFile.prototype.write = function(input, mode) {
var csv = new File(this.getFilePath());
csv.open(mode || "w"); // defaulted to write to file, also allows append
csv.writeln(input); // write your new data to it
csv.close(); // close csv
};
CSVFile.prototype.append = function(input) {
this.write(input, "a"); // append to file
};
CSVFile.prototype.edit = function(input) {
this.edit (input, "e"); //open in edit mode
tabellaDoc.open("e");// open in edit mode
tabellaDoc.seek(0, 2); // go to end of file
var stringa = activeDocument.name + " , " + dataFormattata + " , ";
tabellaDoc.writeln(input);
tabellaDoc.close();
}
var pageItems = doc.layers.getByName("parent").pageItems,
CSV = new CSVFile("Sass vars2"),
canvasWidthHeight = ["CanvasSide", doc.width, doc.height];
CSV.append(canvasWidthHeight);
//for each "div" inside of the parent
for ( var i = 0; i < pageItems.length; i++ ) {
var currentContainer = pageItems[i];
var passedSymbolsWithInfo = [];
// loop through all the symbols to check if it is in the div
for(var j = 0; j < doc.symbolItems.length; j++){
var currentSymbol = new MySymbol(j);
if(currentSymbol.isIn(currentContainer)) {
var currentContainerTopLeft = [Math.abs(currentContainer.top), currentContainer.left];
var symbolRelativeToParent = currentSymbol.getPosition(currentContainerTopLeft[0], currentContainerTopLeft[1]);
var symbolInfo = [currentSymbol.getWidthHeight(), symbolRelativeToParent, currentSymbol.name, currentContainer.name];
passedSymbolsWithInfo.push(symbolInfo);
}
}
print(" ======== end SYMBOL ======== ");
CSV.append(passedSymbolsWithInfo);
}
print("\n ====== end DEV ======== ");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment