Swap library items of an instance on the stage.  I was having some problems with the simple(r) version from FuelJSFL.  Mainly, the instance’s “Instance behavior” setting (changing from a “button” to “movie clip”, for example) is not updated using the dom.swap() method.  Plus Fuel’s won’t work on locked layers.

Pick two library items, and all instances on the stage are swapped from the one item to the other.

Code (cf., for code highlighting):

fl.outputPanel.clear();

var searchType = "instance";
var dom = fl.getDocumentDOM(); 
var lib = dom.library;
var foundItemCount = 0;

var libSel = lib.getSelectedItems();
if(libSel.length < 2){
      fl.trace("ERROR: Select two library items. The first will be to change instances *from*, the second will be to change instances *to*.");
} else {

      libFrom = libSel[0].name;
      libTo = libSel[1].name;
      var libRes = prompt("'" + libFrom + "' to: [x = flip]", libTo);

      var libToIdx = null;

      if(libRes == "x") {
            libFrom = libTo;
            libTo = libSel[0].name;
      } else {
            libToIdx = lib.findItemIndex(libRes);
            libTo = lib.items[libToIdx];
      }

      //libTo = "Revision/ndTroops/ndConfederateTroops";

      fl.trace("RESULTS: \n"
            + "\t libFrom=\"" + libFrom + "\" \n"
            + "\t libTo=\"" + libTo.name + "\" \n"
            + "\t libToIdx=\"" + libToIdx + "\" \n"
      );

      if( libToIdx == null || libTo == null ){
            fl.trace("ERROR: Cannot find library item:\n"
                  + "name sought = \"" + libRes + "\"\n"
                  + "index sought = " + libToIdx + "\n"
                  + "original items = \"" + libSel[0].name + "\"  and  \"" + libSel[1].name + "\"\n"
            );
      } else {
            var results = fl.findObjectInDocByType(searchType, dom);

            fl.trace("*** Found " + results.length + " objects. *** "); 
            // [obj].keyframe = The keyframe that the element is on.
            // [obj].layer = The layer that the keyframe is on.
            // [obj].timeline = The timeline that the layer is on.
            // [obj].parent = The parent of the timeline. E.g., the timeline might be in a symbol instance.

            var lockedStatus;
            //var libFrom = someLibraryItemReference.name;
            //var libTo = otherLibraryItemReference.name;

            if (results.length > 0) { 
                  objsearch: for (var i = 0; i < results.length; i++)
                  {
                        if(results[i].obj.libraryItem.name == libFrom){
                              fl.trace("Found: \n" 
                                    + "\t object = " + results[i].obj.name + "\n"
                                    + "\t keyframe = " + results[i].keyframe.startFrame + "\n"
                                    + "\t layer = " + results[i].layer.name + " (may be nested)\n"
                                    + "\t timeline = " + results[i].timeline.name + ((results[i].parent) ? " (nested)" : "") + "\n"
                                    + "\t parent = " + ((results[i].parent) ? results[i].parent.name : "(none)") + "\n"
                                    + ((results[i].parent) 
                                          ? "\t parent keyframe = " + ((results[i].parent) ? results[i].parent.keyframe.startFrame : "(n/a)") + "\n"
                                                + "\t parent layer = " + ((results[i].parent) ? results[i].parent.layer.name + " (may be nested)" : "(n/a)") + "\n"
                                                + "\t parent timeline = " + ((results[i].parent) ? results[i].parent.timeline.name + " (may be nested)" : "(n/a)") + "\n"
                                          : ""
                                          )
                              );

                              // Cannot select anything on a locked layer.
                              // if, unlock the layer:
                              lockedStatus = results[i].layer.locked;
                              if(results[i].layer.locked) 
                              {
                                    results[i].layer.locked = false;
                              }

                              // select the item:
                              fl.selectElement(results[i],false);

                              // swap with other library item:
                              dom.swapElement( libTo.name );

                              // update the "Instance behaivior" setting on the stage as well.
                              // This is a nasty, tricky little correction that kept me working for a few hours.
                              results[i].obj.symbolType = lib.getItemType(libTo.name);

                              // if, lock the layer:
                              if(results[i].layer.locked != lockedStatus)
                              {
                                    results[i].layer.locked = lockedStatus;
                              }

                              // for testing: break after first result
                              break objsearch;
                        }
                  }
            }
      }
}
Advertisements