Problem setting fill colors with JSFL
I am attempting to write a JSFL script to replace the fill colors of shape elements in selected frames using a mapping table.
I can access a shape's fill properties but I cannot set them even though the documentation leads me to believe these properties are read/write.
When I omit the element.beginEdit() and element.endEdit() calls, the script completes successfully but the colors are not actually updated. When I include the beginEdit/endEdit my script crashes the first time i access the fill property with the error message "The following JavaScript error(s) occurred:" with no other information.
I have tried this in CS5.5, CS6 and CC, and it always fails the same way.
Am I doing something wrong or is it a bug? If it is a bug, has anyone found a work around? I appreciate any tips you can give me.
The script:
var replaceFills = {
'#ff0000': '#33cccc',
'#33cccc': '#ff0000',
'#66ff00': '#00ffff',
'#00ffff': '#66ff00',
'#2173a6': '#ff00ff',
'#ff00ff': '#2173a6',
'#195480': '#ff00ff',
'#ff00ff': '#195480', };
function recolor(element) {
if (element.elementType == "shape") {
element.beginEdit();
for (var j = 0; j < element.contours.length; j++) {
var c = element.contours
if (c.interior) {
if (c.fill.style == 'solid') {
if (c.fill.color in replaceFills) {
c.fill.color = replaceFills[c.fill.color];
}
}
}
}
element.endEdit();
}
}
function recolorFrame(frame) {
var elements = frame.elements;
for (var i = 0; i < elements.length; i++)
recolor(elements);
}
var curSelected = fl.getDocumentDOM().getTimeline().getSelectedFrames();
for (var i = 0; i < curSelected.length; i += 3) {
var layerIndex = curSelected;
var startIndex = curSelected[i + 1];
var endIndex = curSelected[i + 2];
var layer = fl.getDocumentDOM().getTimeline().layers[layerIndex];
for (var j = startIndex; j < endIndex; j++) {
var frame = layer.frames
if (j == frame.startFrame)
recolorFrame(frame);
}
}
Thanks,
Bill
