Script selection/appearance modification causes error 8702
I am using a script to change the appearance of an object (fill color of an ellipse shape). Since there is no way to access appearance properties directly (which would be lovely to get someday), I am using a known workaround with defaultFillColor on the selected document.
app.activeDocument.selection = [item.obj];
app.activeDocument.defaultFillColor = item.newCol;
Unfortunately, the selection seems to leave Illustrator in a weird state, and any further script command (e.g. next iteration of the loop) fails with "Error 8702: there is no active document."
This sometimes goes away if I use CPU instead of GPU preview, though not reliably. And the error is not thrown if there is no update to the fill color (i.e. the object already had the correct color).
This is on version 30.2.1 and macOS 26.3 on apple silicon. I was successfully using the same script for years prior to a few weeks ago, so this might be a recent change. I also tested the latest Illustrator beta, and the error is still there.
I am happy to share the script and illustrator file that trigger the error if needed.
-
Alex Buisse
commented
Thank you Sergey. The issue here seems to be with manipulating the selection, specifically the line
app.activeDocument.selection = [item.obj];
If I remove that line, the script runs fine, though of course doesn't actually do the color replacement I am after. I have tried all sorts of shenanigans to get around the issue, even sending the instructions asynchronously through BridgeTalk, but keep hitting the issue.
I might downgrade to a 2025 version for now, just so I can keep working, as this script is essential to my work.
Edit: ok, I can confirm that the script works in 30.0 and breaks in 30.1/30.2.
-
Sergey Osokin
commented
The error "Error 8702: there is no active document" or another variant "there is no document" occurs not only with defaultFillColor on Mac OS in Adobe Illustrator 2026, but also in rare other types of scripts. On Mac OS with Adobe version 2025 and below, as well as on Windows with Adobe version 2026, there are no problems with your color replacement script.
For example, read a topic from four years ago on a forum about how a collection of open documents breaks down and documents disappears[0]. It seems to me that the problem is similar here, the script and the Illustrator forget that there is an open document.
https://community.adobe.com/questions-652/script-to-save-close-reopen-and-continue-error-8702-there-is-no-document-789390 -
Alex Buisse
commented
Yes, they are shifted here too, it must be from when I deleted a bunch of extraneous layers when exporting the test file. This doesn't seem to be linked to the issue at hand.
I first encountered the error message at the beginning of February, but I don't know where to check when Illustrator did its updates, sorry.
I am not entirely surprised that it would run correctly on a different architecture, since switching to CPU rendering has (at times) solved the issue (though not with this particular file). I am surprised it took so long for you, it usually runs in a couple of seconds at most when it does work.
-
Trying to run the script with the document with routes and circles opened. 30.2.1 on Windows 10 (might be the difference) froze for a couple of minutes and then showed me 'All done' message, no errors thrown. Ellipses changed their colors.
What version you know was the last one to run the script as expected?For some reason the ellipses with numbers are shifted to the left from the lines when I open the doc — take a look at the screenshot... Do these look the same when you reopen the test file you shared?
-
Alex Buisse
commented
And I guess the attachment in the first comment was deleted. Here it is again.
-
Alex Buisse
commented
Ah sorry, it was the script that should be renamed to .txt. Here it is an attachment as well, in case that's easier.
-
Alex Buisse
commented
Here is the script in plain text, and as attachment in the next comment. The issue is with the last few lines.
var doc = app.activeDocument;
var routes = doc.layers["lines"].layers["routes"];
var numbers = doc.layers["lines"].layers["numbers"];function CMYKtoRGB (color) {
try {
var rgb = app.convertSampleColor(ImageColorSpace.CMYK, [Math.round(color.cyan), Math.round(color.magenta), Math.round(color.yellow), Math.round(color.black)], ImageColorSpace.RGB, ColorConvertPurpose.defaultpurpose);
var col = new RGBColor();
col.red = rgb[0];
col.green = rgb[1];
col.blue = rgb[2];
return col;
} catch (e) {alert("Wrong color: " + color)}
}// Calculate relative brightness per https://contrastchecker.online/color-relative-luminance-calculator
function WCAGBrightness (col) {
try {
var r2, r3, g2, g3, b2, b3;
r2 = col.red/255;
g2 = col.green/255;
b2 = col.blue/255
if (r2 <= 0.04045) {
r3 = r2/12.92;
} else {
r3 = Math.pow(((r2 + 0.055)/1.055), 2.4);
}
if (g2 <= 0.04045) {
g3 = g2/12.92;
} else {
g3 = Math.pow(((g2 + 0.055)/1.055), 2.4);
}
if (b2 <= 0.04045) {
b3 = b2/12.92;
} else {
b3 = Math.pow(((b2 + 0.055)/1.055), 2.4);
}
//alert("[" + String(r3) + ", " + String(g3) + ", " + String(b3) + "]");
return ((0.2126 * r3) + (0.7152 * g3) + (0.0722 * b3));
} catch (e) {alert("Wrong color: " + col)}
}// Looks in route list for ID and then stroke color
function findCol(targetID) {
for (var fk = 0; fk < routes.layers.length; fk++) {
for (var fi = 0; fi < routes.layers[fk].pageItems.length; fi++) {
var fname = routes.layers[fk].pageItems[fi].name;
var fids = fname.match("^([0-9]+): ");
if (fids != null) {
var fid = Number(fids[1]);
if (fid == targetID) { return routes.layers[fk].pageItems[fi].strokeColor; }
}
}
for (var fi = 0; fi < routes.layers[fk].layers.length; fi++) {
var fname = routes.layers[fk].layers[fi].name;
var fids = fname.match("^([0-9]+): ");
if (fids != null) {
var fid = Number(fids[1]);
if (fid == targetID) { return routes.layers[fk].layers[fi].pageItems[0].strokeColor; }
}
}
}
return null;
}// Pre-fetch all data before any selection manipulation
var itemsToProcess = [];
for (var n = 0; n < numbers.pageItems.length; n++) {
var obj = numbers.pageItems[n];
var id = Number(obj.contents);
var newCol = findCol(id);
if (newCol == null) { continue; }
var b = WCAGBrightness(CMYKtoRGB(newCol));
var col = new RGBColor();
if (b > 0.4) {
col.red = 0; col.green = 0; col.blue = 0;
} else {
col.red = 255; col.green = 255; col.blue = 255;
}
itemsToProcess.push({obj: obj, col: col, newCol: newCol});
}// Now apply all changes - document access only for selection trick
for (var i = 0; i < itemsToProcess.length; i++) {
var item = itemsToProcess[i];
item.obj.textRange.fillColor = item.col;
app.activeDocument.selection = [item.obj];
app.activeDocument.defaultFillColor = item.newCol;}
alert("All done!");
-
Alex, please do. Renaming a .jsx into .txt should allow you to attach it to a comment, bypassing the UserVoice security.