Skip to main content
brian_p_dts
Community Expert
Community Expert
November 4, 2025
Question

[Script] Problem with changing colors using selection and doc.defaultFillColor

  • November 4, 2025
  • 2 replies
  • 124 views

Hi all, in one of the more recent updates to Illustrator, I have been unable to change a selection's fill color using, for example, the following: 

    var doc = app.activeDocument;
    var sel = app.selection[0];
    var grayCol = new RGBColor();
            grayCol.red = 211;
            grayCol.green = 211;
            grayCol.blue = 211;
        doc.selection = null;
        var defColor = doc.defaultFillColor;
        doc.defaultFillColor = grayCol;

 There error I receive is a PARM error: "there is no document "

I have seen this behavior with a single simple page item selection, an app.executeMenuCommand(

"Find Fill Color menu item") or multiselection.
 
I have seen this across a few different scripts I've written. Think I saw it a couple months ago.  Anyone else seen this and figured out a workaround, other than trying to change the selection's .fillColor property? 

2 replies

brian_p_dts
Community Expert
Community Expert
May 8, 2026

I am still having issues with this across multiple scripts I’ve written. Here is the latest snippet that is not working. It works fine changing the first color, but after that I get “There is no document” error when trying to access the next document layer. Obviously tough to test since I am getting other values from other parts of the code, but like I said in OP,  the problem occurs when I either use “Find Fill Color menu item”, or when I set the default color right after. On Illustrator version 29.8.5 (64-bit), Windows 11 OS build: 26200.8246. If you want I can work up a more complete code base with the other funcs to test. 

     /**
* @description
* @param {Document} doc
*/
this.changeColor = function(doc) {
lm.writeln(3, "Entering changeColor");
var p, c, rgb, cmyk, swatch, layer, pi = undefined;
var crx = /color/i;

for (p in data) {
c = data[p];
p = p.toUpperCase();
if (crx.test(p) && c) {

if (isValidColor(c)) {
try {
layer = doc.layers.getByName(p);
} catch(e) {
lm.writeln(1, "No layer called " + p + ". Skipping color change: " + e);
continue;
}
if (!layer.pageItems.length) {
lm.writeln(2, "No page items in layer " + p + " to change color on.");
continue;
}
rgb = hexToRGB(c);
cmyk = rgbToCmyk(rgb[0], rgb[1], rgb[2]);
swatch = makeColor(cmyk, p);
pi = layer.pageItems[0];
pi.selected = true;
lm.writeln(4, "Do I have a page item selected: " + doc.selection.length);
app.executeMenuCommand("Find Fill Color menu item");
doc.defaultFillColor = swatch;
doc.selection = null;
}
}
}
}

 

brian_p_dts
Community Expert
Community Expert
May 8, 2026

Interesting finding. I added some error logging to see exactly where I lose the doc reference. It seems it occurs after I set the selection to null. I tried both app and doc.selection = null;

 

lm.writeln(4, "Do I have a page item selected: " + doc.selection.length);

app.executeMenuCommand("Find Fill Color menu item");

lm.writeln(4, "Do I have a doc still after app fill: " + doc.name);

doc.defaultFillColor = swatch;

lm.writeln(4, "Do I have a doc still after default color: " + doc.name);

app.selection = null;

lm.writeln(4, "Do I have a doc still after null selex: " + doc.name);

 

brian_p_dts
Community Expert
Community Expert
May 8, 2026

OK, I have worked up a complete test suite. I am getting the layer error on this simple document that I have attached. 
 

var CMTest = {
makeColor : function(cmyk, key) {
//lm.writeln(3, "Entering makeColor");
var c = new CMYKColor();
c.cyan = Number(cmyk.c);
c.magenta = Number(cmyk.m);
c.yellow = Number(cmyk.y);
c.black = Number(cmyk.k);
c.name = key;
return c;
},

/**
* @description Validates that any colors in data are valid hex values. Alerts user of any invalid colors.
* @returns {Boolean} true if all colors in data are valid hex values, false if not. Alerts user of any invalid colors.
*/
isValidColor : function(val) {

var rx = /^[0-9a-f]{6}$/i;
var p = undefined;
c = val.replace(/#/, "");
if (c && !rx.test(c)) {
//lm.writeln(1, "Color " + c + " is not a valid hex color. Skipping color changing.");
return false;
}
return true;
},
/**
* @description Converts a hex color string to an RGB array.
* @param {String} hex A hex color string, with or without a leading #.
* @returns {Array} An array of three integers representing the RGB values of the color.
*/
hexToRGB : function(hex) {
var c = hex.replace(/#/, "");
return [parseInt(c.substr(0,2), 16), parseInt(c.substr(2,2), 16), parseInt(c.substr(4,2), 16)];
},

/**
*
* @param {Integer} r
* @param {Integer} g
* @param {Integer} b
* @returns {Object} An object with properties c, m, y, and k representing the cyan, magenta, yellow, and black values of the color as percentages.
*/
rgbToCmyk : function(r, g, b) {
// Normalize RGB values to 0-1
var r_norm = r / 255;
var g_norm = g / 255;
var b_norm = b / 255;

// Calculate Black (K)
var k = 1 - Math.max(r_norm, g_norm, b_norm);

// If the color is pure black
if (k === 1) {
return { c: 0, m: 0, y: 0, k: 100 };
}

// Calculate Cyan, Magenta, and Yellow
var c = (1 - r_norm - k) / (1 - k);
var m = (1 - g_norm - k) / (1 - k);
var y = (1 - b_norm - k) / (1 - k);

// Return as percentages rounded to nearest integer
return {
c: Math.round(c * 100),
m: Math.round(m * 100),
y: Math.round(y * 100),
k: Math.round(k * 100)
};
},
/**
* @description
* @param {Document} doc
*/
changeColor : function(doc, data) {
//lm.writeln(3, "Entering changeColor");
var p, c, rgb, cmyk, swatch, layer, pi = undefined;
var crx = /color/i;
var oldDefaultColor = doc.defaultFillColor;

for (p in data) {
c = data[p];
p = p.toUpperCase();
alert(p);
if (crx.test(p) && c) {
alert(c);
if (this.isValidColor(c)) {
alert("is valid");
try {
layer = doc.layers.getByName(p);
} catch(e) {
alert("Error getting layer: " + e);
continue;
}
if (!layer.pageItems.length) {
//lm.writeln(2, "No page items in layer " + p + " to change color on.");
continue;
}
rgb = this.hexToRGB(c);
cmyk = this.rgbToCmyk(rgb[0], rgb[1], rgb[2]);
swatch = this.makeColor(cmyk, p);
layer.hasSelectedArtwork = true;
//lm.writeln(4, "Do I have a page item selected: " + doc.selection.length);
//app.executeMenuCommand("Find Fill Color menu item");
//lm.writeln(4, "Do I have a doc still after app fill: " + doc.name);
doc.defaultFillColor = swatch;
doc.selection = null;
//lm.writeln(4, "Do I have a doc still after default color: " + doc.name);
//Utils.deselectItems(doc.selection);
//lm.writeln(4, "Do I still have a doc after deselecting: " + doc.name);

}
}
}
try {
doc.defaultFillColor = oldDefaultColor;
} catch(e) {
alert("Is this the bug with color change?");
}
}

}

CMTest.changeColor(app.activeDocument, { backgroundColor: "111111", overlayColor: "222222", color2: "333333"});

 

CarlosCanto
Community Expert
Community Expert
November 4, 2025

that sample script works fine here. I'm on Windows 11, Illustrator v30.0