Skip to main content
9a0T4NgFmg
Participant
June 4, 2014
Question

Problem setting fill colors with JSFL

  • June 4, 2014
  • 1 reply
  • 2132 views

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

This topic has been closed for replies.

1 reply

Amy Blankenship
Legend
June 5, 2014

Is it possible that you're trying to access the fill of a contour with a null fill? I don't see any null checks in your code.

9a0T4NgFmg
Participant
June 5, 2014

Thanks for the tip but even accessing the fill field like if(!c.fill) fl.trace("Null fill"); results in the same error. The contour c is definitely not null because the c.interior check works.

Amy Blankenship
Legend
June 6, 2014

Just to make sure I'm understanding you--you've tried it with just

if (!c.fill) {

     fl.trace('null fill');

}

and you've removed all references to c.fill.color and c.fill.style? And that still errors?

Also, what happens if you remove the empty element from your array?