I am attempting to write a JSFL script that will replace all contour fills that match given colors on selected frames.
My problem is that if I call shape.beginEdit() and shape.endEdit() as specified by the JSFL documentation, the script crashes whenever I access a fill attribute on a contour (even read only access). The error message is "The following JavaScript error(s) occurred:" with no additional information.
If I omit the shape.begin/endEdit() calls, the script does not crash but the colors are not actually updated.
I have tried running this script in Flash CS5.5, CS6 and CC with the same results.
I would appreciate it if someone could point out what I am missing.
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[j];
if (c.interior && c.fill) {
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[i]);
}
var curSelected = fl.getDocumentDOM().getTimeline().getSelectedFrames();
for (var i = 0; i < curSelected.length; i += 3) {
var layerIndex = curSelected[i];
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[j];
if (j == frame.startFrame)
recolorFrame(frame);
}
}