Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Created June 19, 2012 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilukasz/2951981 to your computer and use it in GitHub Desktop.
Save hilukasz/2951981 to your computer and use it in GitHub Desktop.
// constructors and prototypes
// can probably ignore this, it definitely works and has been tested
function CSVFile(fileName) {
this.fileName = fileName;
};
CSVFile.prototype.getFilePath = function() {
return inputFolder + "/" + this.fileName + ".csv";
};
CSVFile.prototype.append = function(input) {
this.write(input, "a"); // append to file
};
var doc = app.activeDocument,
mainParentContainer = doc.layers.getByName("parent").pageItems,
CSV = new CSVFile("Sass vars"),
inputFolder = Folder.selectDialog(),
canvasWidthHeight = [doc.width, doc.height, "", "", "", "Window"],
symbolPattern = "[child] ", // child but parent of symbols
allParentsOfSymbols = getArrayOfItemsWithNameUnder(symbolPattern, mainParentContainer),
iCount = doc.symbolItems.length,
dateTime = getTodaysDate(),
dest = undefined,
dest,
win;
function getAllTextItems(){
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
var doc = app.activeDocument;
for(var j = 0; j < doc.textFrames.length; j++){
var currentTextItem = doc.textFrames[j],
font = currentTextItem.textRange.characterAttributes.textFont.name,
style = currentTextItem.textRange.characterAttributes.textFont,
alignment = currentTextItem.paragraphs[0].paragraphAttributes.justification,
fontSize= currentTextItem.textRange.characterAttributes.size,
contents = currentTextItem.contents,
myColor = Math.round(app.activeDocument.textFrames[j].textRange.characterAttributes.fillColor.red)+" "+Math.round(app.activeDocument.textFrames[j].textRange.characterAttributes.fillColor.green)+" "+Math.round(app.activeDocument.textFrames[j].textRange.characterAttributes.fillColor.blue),
alignment = alignment.toString(),
alignment = alignment.replace('Justification.','');
//temporarly render text to get position, then remove
var temporaryText = currentTextItem.duplicate().createOutline(),
left = Math.round(temporaryText.visibleBounds[0]),
top = Math.abs(Math.round(temporaryText.visibleBounds[1])),
right = Math.round(temporaryText.visibleBounds[2]),
bottom = Math.abs(Math.round(temporaryText.visibleBounds[3]));
temporaryText.remove();
// loop through all pageItem containers
for(var i = 0; i < allParentsOfSymbols.length; i++){
var container = allParentsOfSymbols[i],
containerVB = container.visibleBounds,
containerLeft = containerVB[0],
containerTop = Math.abs(Math.round(containerVB[1])),
containerRight = containerVB[2],
containerBottom = Math.abs(Math.round(containerVB[3]));
if(textIsIn(currentTextItem, container)) {
var relativeBottom = containerTop-bottom;
if(alignment == "LEFT") { var leftCenterRight = left-containerLeft; }
if(alignment == "CENTER") {
var width = right-left,
textCenter = width/2+left,
leftCenterRight = textCenter-containerLeft;
}
if(alignment == "RIGHT") { var leftCenterRight = containerRight-right; }
CSV.append(["null","null", Math.abs(relativeBottom), leftCenterRight, contents, makeWebsafe(container.name.replace(symbolPattern, "")), "text", myColor , fontSize, alignment,font, style, getTodaysDate()+"\n"]);
}
}
}
}getAllTextItems();
function textIsIn(textChild, parentPageItem){
var textVB = textChild.visibleBounds,
textLeft = textVB[0],
textTop = Math.abs(textVB[1]),
textRight = textVB[2],
textBottom= Math.abs(textVB[3]);
var parentVB = parentPageItem.visibleBounds,
parentLeft = parentVB[0],
parentTop = Math.abs(parentVB[1]),
parentRight = parentVB[2],
parentBottom = Math.abs(parentVB[3]);
//return true if child is in parent
return textLeft >= parentLeft && textRight <= parentRight &&
textBottom <= parentBottom && textTop >= parentTop;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment