PDA

View Full Version : make your flas nice


psytraxx
08-29-2004, 07:50 PM
Inspired by the previous post i wrote a helpful JSFL script to clean up the flas from my designers and to prepare them for scripting...

Features

Export linked symbols to a selected frame
Places all MCs with a linkage id at frame 3 inside a new created layer named exports. Disables export to first frame if set in MC properties.

Rename library elements
This JSFL script adds aditional suffixes for MCs ("_mc"), buttons ("_btn") and graphic clips ("_g"). Additonal all sounds and bitmaps will be moved to a new directory ("_sounds" or "_bitmaps").


Make static texts dynamic and save their previous values as XML
Checks for static text in all document and library items timelines. Changes the text to dynamic, set an instance name and output an xml with all former static text values.

Name Layers and give MC instances an identfier
I hate layers called "Layer XY" or "Ebene XY" - the script will assign the name of the first found mc to the layer name. Also it looks for unnamed MC instances and sets the indentifier to the MCs name.

At the moment i am writing a little user interface to build an extension.
Please feel free to report any bugs and give me improvment hints!

Psytraxx

function TextCollector () {
var textIterator = 0;
var textObj = new Object();

this.addText = function (_value,_key) {
var keyname = "";
if (_key == undefined || _key =="") {
keyName = "text"+textIterator +"_txt";
textIterator ++;
} else {
keyName = _key;
}
fl.trace("(TextCollector).addText " + _value +":" + keyName);
textObj[keyName] = _value;
return keyName;
}
this.getAllTextsAsXML = function () {
fl.trace("(TextCollector).getAllTextsAsXML ");
var xmlout = "";
for (var i in textObj) {
xmlout +="<text key='"+i+"' value='"+textObj[i]+"' />\n";
}
return xmlout;
}

}

function MakeTheDeveloperHappy(_docr) {
var docRef = "";
var allTexts = new TextCollector();

this.createExportLayerIfneeded = function () {
fl.trace("createExportLayerIfneeded " + arguments);
var currentLayers = docRef.getTimeline().layers;
var found = false;
for (var k=0;k<currentLayers.length;k++) {
if (currentLayers[k].name =="export") {
found =true;
docRef.getTimeline().currentLayer = k;
}
}
if (!found) {
docRef.getTimeline().addNewLayer("export");

}
// create nessary frames
var mytimeLine = fl.getDocumentDOM().getTimeline();
mytimeLine.clearKeyframes(2);
mytimeLine.insertKeyframe(2);
mytimeLine.clearKeyframes(3);
mytimeLine.insertKeyframe(3);
mytimeLine.setSelectedFrames(2,2);
mytimeLine.layers[mytimeLine.currentLayer].frames[2].name = "export";


}

this.disableExportInFirstFrame = function (_mclibitem) {
fl.trace("disableExportInFirstFrame " + arguments);
var isMarkedforExport = _mclibitem.linkageExportForAS
// check if exported to first frame
if (isMarkedforExport) {
_mclibitem.linkageExportInFirstFrame = false;
}
return isMarkedforExport;

}

this.getNameWithoutPath = function (_name) {
var namePartsArray = new Array();
namePartsArray = _name.split("/");
return namePartsArray[namePartsArray.length-1]
}


this.addPrefixToLibraryItem= function (_item,_prefix) {
fl.trace("addPrefixToLibraryItem " + _item +","+_prefix);
if(_item.name.indexOf(_prefix) == -1) {
_item.name = this.getNameWithoutPath(_item.name) +_prefix;
fl.trace("new name: "+_item.name);
}
}

this.moveItemToFolder = function (_item,_foldername) {
fl.trace("moveItemToFolder " + _foldername);
docRef.library.newFolder(_foldername);
docRef.library.moveToFolder(_foldername,_item.name , true);
}


this.addPrefixToAllLibraryItems= function (_itemnode) {
fl.trace("addPrefixToAllLibraryItems " + _itemnode);
for (var i = 0; i < _itemnode.length; i++) {
var myLibItem = _itemnode[i];
fl.trace("process item: " + myLibItem.name);
switch (myLibItem.itemType) {
case "graphic" :
this.addPrefixToLibraryItem(myLibItem,"_g");
break;
case "button" :
this.addPrefixToLibraryItem(myLibItem,"_btn");
break;
case "movie clip":
this.addPrefixToLibraryItem(myLibItem,"_mc");
if (this.disableExportInFirstFrame(myLibItem)) {
docRef.addItem( {x:0,y:0}, myLibItem)
}
break;
case "bitmap":
this.moveItemToFolder(myLibItem,"_bitmap");
break;
case "folder" :
// dive rekursive into folders
this.addPrefixToAllLibraryItems(myLibItem)
break;
case "sound" :
this.moveItemToFolder(myLibItem,"_sounds");
break;
}
}
}

this.checkTimeLine = function (_timeline) {
fl.trace("checkTimeLine");
for (var k = 0; k < _timeline.layers.length;k++) {
var myLayer = _timeline.layers[k];
// dont proccess folders
if (myLayer.layerType != "folder" ){
var lastSymbolName = "";
fl.trace("Processing layer: "+myLayer.name);
//
var oldTextElementName = "";
// loop through frames
for (var l = 0; l < myLayer.frameCount;l++) {
var myFrame =myLayer.frames[l];
// loop throught elements
for (var i = 0;i < myFrame.elements.length; i++) {
var myElement = myFrame.elements[i];
switch (myElement.elementType) {
case "text":
// rename only the first position found, or if empty name or value,
// dont rename exisiting _txt instances && myElement.name.indexOf("_txt") == -1
if (( myElement.name !=lastSymbolName || myElement.name =="") && myElement.getTextString() !="" && myElement.getTextString().indexOf("_txt") < 0 ) {
// set text dynamic and format
myElement.textType = "dynamic";
myElement.selectable = false;
myElement.embedRanges = "1|2|3|4";
myElement.lineType = "multiline";
//myElement.autoExpand = true;
myElement.embeddedCharacters = "";
myElement.name = allTexts.addText(myElement.getTextString(),myEleme nt.name);
myElement.variableName = "_global."+myElement.name;
myElement.setTextString(myElement.name)
lastSymbolName = myElement.name;
}
break;
case "instance" :

if (myElement.instanceType=="symbol") {
var libName = this.getNameWithoutPath(myElement.libraryItem.name );
if (myElement.name=="") {
myElement.name = libName;
}
lastSymbolName = myElement.name;
// mcs could have timelines too
this.checkTimeLine(myElement.libraryItem.timeline) ;
}
break;
}

}
}
// check layer if layer name is default
if ((myLayer.name.indexOf("Layer") > -1 || myLayer.name.indexOf("Ebene") > -1 )&& lastSymbolName != "" ) {
myLayer.name = lastSymbolName;
}
}
}
}

// n
this.nameAllUnamedInstances = function () {
fl.trace("nameAllUnamedInstances");
// get timeline of document and
var mytimeline = docRef.getTimeline();
// loop through layers of the given timeline
this.checkTimeLine(mytimeline);
}
//
this.init = function (_docr) {
//outputPanel.clear()
docRef = _docr;
this.createExportLayerIfneeded();
this.addPrefixToAllLibraryItems(_docr.library.item s);
this.nameAllUnamedInstances();
}
this.getXML = function () {
var oxml = allTexts.getAllTextsAsXML();
return oxml;
}
}

// main function
function initMain () {
var btdh = new MakeTheDeveloperHappy();
btdh.init(fl.getDocumentDOM());
return btdh.getXML()
}

fl.trace(initMain());