This tutorial assumes that you are fairly clear with the basics of JSFL and the JSAPI provided as a part of Flash MX 2004 release. If not, you may refer to the "Introduction to JSAPI" tutorial in the tutorials section.
In this tutorial we will write a jsfl file, which will convert the library into a well-organized library. Usually the designers/developers tend to ignore Library of the FLA file and end up cluttering it linearly with no folder structure. At a later stage it is difficult and time consuming to arrange the library to make it look better so that finding objects is easier.
The jsfl "command" we intend to create comes handy in this situation. This command creates five folders viz., Audio, Buttons, Graphics, Movie Clips and Miscellaneous . It then picks each item and puts in the corresponding folder. So in a larger project FLA where the number of elements is large, the arranging is quick and in one shot. Also, it does not make changes or move any of the user created folders to avoid confusion.
Ok, lets start. Open any text editor of your choice. If you have Macromedia Flash professional version, you will be able to create the jsfl file using Flash directly (and consequently get help and full access to auto complete dropdowns). I am using Flash MX 2004 Pro to create the jsfl file. The editor here has only the text editor available in this mode. All other panels are disabled. See image below Fig 1.1, depicting the screenshot of the Flash jsfl editor (Notice that only the text editor is enabled in this mode):

Fig 1.1
The Object we will use here is the Library object that represents the library of the currently open Flash file. So lets first create an instance of the same as follows:
//create an instance of library object
mylibrary = fl.getDocumentDOM().library;
Notice that Library Object is the child of the top level 'fl' or 'flash' object hhence is derived from the document DOM.
Next, we create the folders we need to put our items in. We do this by invoking the "newFolder" method for the library object as follows:
//create organization folders
fl.getDocumentDOM().library.newFolder("Audio");
fl.getDocumentDOM().library.newFolder("Buttons");
fl.getDocumentDOM().library.newFolder("Graphics");
fl.getDocumentDOM().library.newFolder("Movie Clips");
fl.getDocumentDOM().library.newFolder("Miscellaneous");
This will create the required 5 folders in the Library. These folders will be empty initially. The folders behave similar to windows. If they exist already, they are not deleted and recreated and any items within them remain intact.
Next we identify the items in the library and their count.
//get items in library in an Array
itemArray = mylibrary.items;
totalCount = itemArray.length;
The itemArray represents an Array of all items in the Library. Thus, the variable totalCount is the number of items in the Library.
Next we loop through the Library to check the type of each item. Depending the type of item we put the items in the respective folders that we created above. Thus,
myItemType = myItem.itemType;
myItemName = myItem.name;
As obvious, the properties itemType and name are used to identify the type and name of the item in the library.
We use the library object method moveToFolder to move the library item from root to the desired folder as follows:
mylibrary.moveToFolder("Graphics", myItemName, true);
The complete "for" loop that runs on the Library looks as follows:
//Comments : Begin
//iterate through the Library items in the library
//iIdentify type of the item and move it to the respective folder
//The miscellaneous category represents anything that is not a graphic or movie clip // or button - if the item type is folder it is ignored
//The miscellaneous item could be another folder or component
//Comments : End
for(var i=0;i
myItemType = myItem.itemType;
myItemName = myItem.name;
fl.outputPanel.trace(myItemType +">>"+myItem);
if(myItemType == "sound"){
mylibrary.moveToFolder("Audio", myItemName, true);
}else if(myItemType == "button"){
mylibrary.moveToFolder("Buttons", myItemName, true);
}else if(myItemType == "graphic"){
mylibrary.moveToFolder("Graphics", myItemName, true);
}else if(myItemType == "movie clip"){
mylibrary.moveToFolder("Movie Clips", myItemName, true);
}else if(myItemType == "folder"){
}else{
mylibrary.moveToFolder("Miscellaneous", myItemName, true);
}
}
The loop uses the if..else if..else conditional statements to put library items into respective folders depending on the type. A switch statement could have also been used here to replace if..else if..else loop.
Finally, We save this file at an appropriate location. If you want this to be available as a command under the "Commands" drop down menu the next time you start Flash, save the jsfl in the folder:
In my case it is:
C:Program FilesMacromediaFlash MX 2004enFirst RunCommands
Cool, now that we are through with the code lets see it in action. Open any FLA file, which you feel, needs "organizing". Go to "Commands > Run Command". A "browse file" type dialog pops up. Choose the recently created jsfl file. And see the folders getting created automatically and appropriate elements being moved to appropriate folders.
The jsfl file "organize_library.jsfl" is available along with this tutorial.
As an extension to this simple "macro", you may also use the Library object to arrange the library in an alphabetical order, or remove specific type of items from the library like sounds. You may also want to update all the items before the each publish. You may also modify it to rename all library items to have a prefix attached to each item name. Download Source File
discuss this topic to forum
