I need some help. I have a script that's been great for preparing files for pre-press. I added a function to it recently that sets objects that use spot swatches with a "_" at the start of their name to overprint. I discovered that stray anchor points for some reason break this function.
Easy enough from the menu, Select > Object > Stray Points. Unfortunately from what I can tell, it's not so easy in script form. So it has to loop through every single path.
In smaller files, not a big deal and it works. Some of my files have CAD drawn objects though with include so many small paths. The looping takes forever on these. I only have a base understanding of what all the script is doing, so I could use help trying to optimize how this analyzes both the stray anchor points as well as looping through the objects that need to be set to overprint. If anyone has suggestions on how to clean this up, I'm all ears. Thanks!
function applyOverprint() {
updateStatus('Preparing Overprint'); w.update();
var allSwatches = doc.swatches;
var straypoints = [];
// Collect stray points
var allPageItems = doc.pageItems;
for (var j = 0; j < allPageItems.length; j++) {
var item = allPageItems[j];
if (item.typename === "PathItem" && item.pathPoints.length === 1) {
straypoints.push(item);
} else if (item.typename === "TextFrame" && item.textRange.length === 0) {
straypoints.push(item);
}
}
// Remove stray points
for (var l = 0; l < straypoints.length; l++) {
var strayItem = straypoints[l];
if (strayItem instanceof PathItem) {
strayItem.remove();
} else if (strayItem instanceof TextFrame) {
strayItem.remove();
}
}
function hasUnderscore(name) {
return name && name.charAt(0) === "_";
}
function applyOverprintToItems(items) {
updateStatus('Applying Overprint to Items'); w.update();
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (item.typename === "GroupItem" || item.typename === "CompoundPathItem") {
if (item.pageItems) {
// Recursively process groups and compound paths
applyOverprintToItems(item.pageItems);
}
} else {
// Process other items
if (item.typename === "TextFrame") {
applyOverprintToTextFrame(item);
} else {
applyOverprintToPathItem(item);
}
}
}
}
function applyOverprintToTextFrame(textFrame) {
updateStatus('Applying Overprint to Text'); w.update();
var textAttributes = textFrame.textRange.characterAttributes;
if (textAttributes.fillColor && textAttributes.fillColor.spot && hasUnderscore(textAttributes.fillColor.spot.name)) {
textAttributes.overprintFill = true;
}
if (textAttributes.strokeColor && textAttributes.strokeColor.spot && hasUnderscore(textAttributes.strokeColor.spot.name)) {
textAttributes.overprintStroke = true;
}
}
function applyOverprintToPathItem(pathItem) {
updateStatus('Applying Overprint to Paths'); w.update();
if (pathItem.filled && pathItem.fillColor && pathItem.fillColor.spot && hasUnderscore(pathItem.fillColor.spot.name)) {
pathItem.fillOverprint = true; // Apply fill overprint
}
if (pathItem.stroked && pathItem.strokeColor && pathItem.strokeColor.spot && hasUnderscore(pathItem.strokeColor.spot.name)) {
pathItem.strokeOverprint = true; // Apply stroke overprint
}
}
for (var s = 0; s < allSwatches.length; s++) {
var swatch = allSwatches[s];
if (swatch.spot && hasUnderscore(swatch.name)) {
swatch.color.overprint = true;
}
}
applyOverprintToItems(doc.pageItems);
}