3

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);
    }
}

3 Answers3

3

Edit yor function just like this:

function recolor(element) {
if (element.elementType == "shape") {
//del   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) {
//del                   c.fill.color = replaceFills[c.fill.color];
//add this
                        var fill = element.getCustomFill();
                        fill.style = "solid";
                        fill.color = replaceFills[c.fill.color];
                        element.setCustomFill(fill);
//end
                    }
                }
            }
        }
//del   element.endEdit();
     }
  }
2

FWIW, one can also replace colors using the Find & Replace panel, located under Edit.

blvz
  • 1,335
  • 13
  • 15
0

Mike's answer did change the colour, but unfortunately if a shape is made up of multiple contours with different colours, they are all changed.

Instead, getting the fill, changing the colour, and the reassigning the fill to the contour does the trick.

var fill = contour.fill;

if(fill.color in replaceFills)
{
    fill.color = replaceFills[fill.color];
    contour.fill = fill;
}
cmann
  • 1,920
  • 4
  • 21
  • 33