0

I want to swap a bitmap in symbol with another bitmap,and I found the the function

swapElement

This is what I have tried

var elements = item.timeline.layers[0].frames[0].elements;//item is the symbol

for (var j = 0; j < elements.length; j++) {

    var el = elements[j];

    if (el.instanceType == "bitmap") {
        el.selected = true;//this line not work, so I want a way to make the element selected

        dom.swapElement(targetName);//targetName is another bitmap name that I wanted
    }
} 
Pan
  • 2,101
  • 3
  • 13
  • 17

2 Answers2

0

From library? Then following snippet could help you

var libItems = fl.getDocumentDOM().library.items;

for (i = 0; i < libItems.length; i++){
if(libItems[i].itemType == “bitmap”){
//found bitmap :)
}

OK, what about:

fl.selectElement(el); //instead of el.selected = true;

dom.swapElement(targetName);//assuming you have targetName

This works if there is anything (one item) selected that is bitmap it will replace it with library item named "image4":

var dom = fl.getDocumentDOM();
var selection = dom.selection;

if(selection.length == 1 && selection[0].instanceType == "bitmap")
{
    dom.swapElement("image4");
}
Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79
  • Sorry, I haven't make my question clear, I have edit the question. – Pan Oct 23 '13 at 08:06
  • I tried this and it gives me an error TypeError: elementData.timeline has no properties. So I guess is there any code that do the same job as I click on the bitmap. I have watched the command in history panel, it is a mouse click command only. – Pan Oct 23 '13 at 08:25
  • you can add more details about your setup, where is bitmap that you are trying to swap with etc. – Lukasz 'Severiaan' Grela Oct 23 '13 at 09:21
  • Your last answer is based on the bitmap has been selected, but what I want is how to make the bitmap selected. – Pan Oct 24 '13 at 06:22
0

Setting selected to true of an element, will only select the element if the element is on screen (and the layer is unlocked and visible). So before selected is set to true library.editItem(item.name) should be called. If the element is in frame x, frame x has to be made the current frame with document.getTimeline().setSelectedFrames(parseInt(x), parseInt(x) + 1, true). (I use parseInt(x) as a cast because of a bug in setSelectedFrames). To be sure the element is the only element that is selected document.selectNone() should be called before setting selected to true.

Every instance has a libraryItem so maybe replacing the libraryItem is easier. The code here can be adapted for a more selective replace.

Community
  • 1
  • 1
Ron700
  • 66
  • 1
  • 4
  • Replacing the libraryItem works.Is there any way to make the element selected? I have use edititem(targetItem) and use setSelectedFrames. Then I use el.selected = true,but when I print the selected item, it's the targetItem. – Pan Oct 24 '13 at 15:04