Skip to content

Instantly share code, notes, and snippets.

@rasteiner
Last active March 17, 2016 19:22
Show Gist options
  • Save rasteiner/000d13bc9adedd747651 to your computer and use it in GitHub Desktop.
Save rasteiner/000d13bc9adedd747651 to your computer and use it in GitHub Desktop.
/// illustrator script stub - converts paths to ... something else
var items = app.activeDocument.pageItems;
var strlog = '';
var ops = [];
function log(str) {
strlog+= "\n" + str;
}
function pathOP(item) {
var points = item.pathPoints, p, s, c1, c2, str;
//move to first point
var str = "M" + fc(item.pathPoints[0].anchor);
for(var i = 0; i < item.pathPoints.length; i++) {
//this is a closed shape or simply not the last point
if(i < points.length - 1 || item.closed) {
//draw a line to next point
s = fc(getPoint(item, i).anchor);
c1 = fc(getPoint(item, i).rightDirection);
c2 = fc(getPoint(item, i+1).leftDirection);
p = fc(getPoint(item, i + 1).anchor);
if(s == c1 && p == c2) {
//control points are equal to their anchor - it's a straight line
//this also happens to be the last segment, just let the final "z" close it
if(i == points.length - 1) break;
//else draw the line
str += "L";
} else if(c1 == c2) {
//control points happen to be the same, make a quadratic bezier
str += "Q" + c1 + " ";
} else {
//it's a cubic bezier
str += "C" + c1 + " " + c2 + " ";
}
str += p;
}
}
//close closed paths, just to be sure
if(item.closed) str += 'z ';
return '["path",["' +str + '"]]';
}
function fillOP(color) {
var str;
if(color.typename == 'SpotColor') {
str = '[' + color.spot.color.cyan + ',' + color.spot.color.magenta + ',' + color.spot.color.yellow + ',' + color.spot.color.black + ']';
} else if(color.typename == 'GrayColor') {
str = '[0,0,0,' + (color.gray) + ']';
} else if(color.typename == 'CMYKColor') {
str = '[' + color.cyan + ',' + color.magenta + ',' + color.yellow + ',' + color.black + ']';
} else if(color.typename == 'RGBColor') {
str = '[' + color.red + ',' + color.green + ',' + color.blue + ']';
}
return '["fill",[' + str + ']]';
}
function getPoint(p, index) {
return p.pathPoints[index % p.pathPoints.length];
}
function fc(coord) {
return coord[0] + ',' + (-coord[1]);
}
function hPathItem(item) {
if(item.hidden) return;
ops.push(pathOP(item));
item.hidden = true;
}
var item, i, j;
for(i = 0; i < app.activeDocument.pathItems.length; i++) {
item = app.activeDocument.pathItems[i];
if(item.hidden) continue;
if(item.parent.typename == 'CompoundPathItem') {
for(j = 0; j < item.parent.pathItems.length; j++) {
hPathItem(item.parent.pathItems[j]);
}
} else {
hPathItem(item);
}
if(item.filled) {
ops.push(fillOP(item.fillColor));
}
}
for(var i = 0; i < app.activeDocument.pathItems.length; i++) {
item = app.activeDocument.pathItems[i];
item.hidden = false;
}
var textFile = File('~/Desktop/ops.json');
textFile.open('e');
textFile.write('[');
textFile.write(ops.join(',\n'));
textFile.write(']');
textFile.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment