Flash CS4 JSFL: Replace All Symbols in Frame 0 With a Library Item
Often when I prototype things in Flash, I use placeholder shapes to quickly try various layout ideas. I did this the other day with the a side-burner project mine: my first Art Geek Zoo specific game. After a few iterations I landed on a good layout. From there I started to turn the placeholder items into real components.
Swapping out the placeholders for real components is simple enough when there's not many instances of a given component. I had this one component that had 100 placeholder shapes within it. To avoid the monotony, I wrote this script. In order to use it in your Flash project (works in CS4, probably works in CS3 as well):- Isolate the shapes/elements you wish to swap out for a given library component into their own symbol by selecting them, and pressing "F8".
- Edit that symbol.
- Run the script.
- You'll be prompted for the name of the item in your Library you want to swap instances... enter that.
- Let the script do its thing...
var doc = fl.getDocumentDOM();
var lib = doc.library; // helper function, get a library item by its name
getLibraryItemByName = function(itemName){
libraryList = lib.items
if(libraryList.length != 0)
{
for(var iLib=0; iLib<libraryList.length; iLib++){
if(lib.items[iLib].name == itemName){
return lib.items[iLib];
break;
}
}
}
return undefined;
} //main function
main = function(){
var libraryItemName = prompt("Enter Library Item Name to use to replace the current symbols.", "?");
if(getLibraryItemByName(libraryItemName) == undefined){
alert(libraryItemName + " not found, stopping script.");
}
else{
layersList = fl.getDocumentDOM().getTimeline().layers;
elementsList = fl.getDocumentDOM().getTimeline().layers[0].frames[0].elements;
coordinatesList = [];
// gather coordinates
for(var iLayer=0; iLayer<100; iLayer++) {
for(var iElement=0; iElement<elementsList.length; iElement++) {
try{
var currentElement = fl.getDocumentDOM().getTimeline().layers[iLayer].frames[0].elements[iElement];
try{
coordinatesList.push({x:parseFloat(currentElement.x), y:parseFloat(currentElement.y)});
}
catch(error){}
}
catch(error){}
} // end for elementsList
} // end for layersList
// match elements to coordinates, replace them with new library choice
for(var iCoord=0; iCoord<coordinatesList.length; iCoord++){
var replaceDone = false;
var itemRefs = {};
var coord = coordinatesList[iCoord];
var xPos = parseFloat(coord["x"]);
var yPos = parseFloat(coord["y"]);
for(var iLayer=0; iLayer<layersList.length; iLayer++) {
if(replaceDone==true){
break;
}
for(var iElement=0; iElement<elementsList.length; iElement++) {
if(replaceDone==true){
break;
}
try{
var currentElement = fl.getDocumentDOM().getTimeline().layers[iLayer].frames[0].elements[iElement];
try{
if(currentElement.x == xPos
&& currentElement.y == yPos){
var itemRefName = String(xPos)+"_"+String(yPos);
itemRefs[itemRefName] = getLibraryItemByName(libraryItemName);
document.selectNone();
fl.getDocumentDOM().selection = [currentElement];
document.deleteSelection();
document.selectNone();
fl.documents[0].addItem({x:parseFloat(coord["x"]), y:parseFloat(coord["y"])}, itemRefs[itemRefName]);
fl.getDocumentDOM().selection[0].x = xPos;
fl.getDocumentDOM().selection[0].y = yPos;
replaceDone = true;
break;
}
}
catch(error){}
}
catch(error){}
} // end for elementsList
} // end for layersList
} // end for coordinatesList
}// end if
}// end main
// run the script:
main();
</code>