This script is used to make a few changes that SOME of our die suppliers require. It works great. If you have a little time to look it over and give me some advice on best practices, consiseness etc. it would be greatly appreciated.
Note: The files this script is aimed at are fairly civilized, and don't vary much so I didn't have to buld in room for varience. For example, the die lines will ALWAYS be a spot color, so I didn't need to include anything for RGB or CMYK.
// script.name = prepDieFile.js; // script.description = removes unnecessary layers, vectors, and text. Leaves cross-mark line unpainted. Colors all remaining text and strokes black;// script.requirements = an opened document;// script.parent = elDudereno // 10/10/13;// script.elegant = false;
#target Illustrator
var idoc = app.activeDocument;
var pi = idoc.pathItems;
var tf = idoc.textFrames;
var deletedLayers = 0;
var deletedPaths = 0;
var spotStroke = 0;
var deletedText = 0;
var blackedText = 0;
var layerCount = idoc.layers.length;//loop through all layers deleting hidden layers. Loop from the back, to preserve index of remaining layers when we remove one.for (var i = layerCount - 1; i >= 0; i--) { var thisLayer = idoc.layers[i]; if (thisLayer.visible == false || thisLayer.locked == true) { thisLayer.visible = true; thisLayer.locked = false; thisLayer.remove(); deletedLayers++; }}
unlockPaths(); //locate largest path item (bounding box) and lock it.
var theBiggest = pi[0];for (i=1; i<pi.length; i++) { if (Math.abs(pi[i].area) > Math.abs(theBiggest.area)) { theBiggest = pi[i]; }}
theBiggest.remove();
deletedLayers++;//locate second largest path (cross-mark size) remove stroke and lock
var secondBiggest = pi[0];for (j=1; j<pi.length; j++) { if (Math.abs(pi[j].area) > Math.abs(secondBiggest.area)) { secondBiggest = pi[j]; }}
secondBiggest.strokeColor = NoColor;
secondBiggest.fillColor = NoColor;
secondBiggest.locked = true; // loop through path items delete if hidden, registration, or un-stroked. Change all non-registration spot colors to 100% GrayColor.for (var j=pi.length -1; j >= 0; j--) { var ipath = pi[j]; if (ipath.hidden==true) { ipath.remove(); deletedPaths++; } if (ipath.locked==false) { var strokeColor = ipath.strokeColor; if (strokeColor.typename == "NoColor"){ ipath.remove(); deletedPaths++; } elseif (strokeColor.typename == "SpotColor"){ if (strokeColor.spot.name == "[Registration]"){ ipath.remove(); deletedPaths++; }else{ ipath.strokeColor = GrayColor; ipath.strokeColor.gray = 100.0; spotStroke++; } } }}
unlockPaths(); // delete text frames with first letter set to registration. Turn all other text to black.for (t=tf.length -1; t>=0; t--) { var iTxtFrm = tf[t]; var firstLtr = iTxtFrm.characters[0]; if (firstLtr.characterAttributes.fillColor.spot.name == "[Registration]"){ iTxtFrm.remove(); deletedText++; } else{ var chars = iTxtFrm.characters; for (var c=0; c<chars.length; c++) { var ltr = chars[c]; ltr.characterAttributes.fillColor = GrayColor; ltr.characterAttributes.fillColor.gray = 100.0; blackedText++; } }}
redraw();
alert(deletedLayers + " hidden or locked layer(s), " + deletedPaths + " path(s) & " + deletedText + " text frames, were deleted. " + blackedText + " letters, & " + spotStroke + " stroke(s) were converted to 100% K."); //unlock all path items
function unlockPaths(){ for (var k=0; k<pi.length; k++){ var lpath = pi[k]; if (lpath.locked == true){ lpath.locked = false; } }}
Thanks for taking a look.
http://www.filedropper.com/0388701die<-sample file.