Looping over items in your Flash document’s Library panel using JSFL

by Peter deHaan on November 25, 2008

in JSFL

 The following example shows how you can create a simple JSFL file which loops over each of the items in your Flash document’s Library panel and displays the item’s name and item type in the Output panel.

Full code after the jump.

// JSFL
var items = fl.getDocumentDOM().library.items;
fl.outputPanel.clear();
if (items.length > 0) {
    for each (var item in items) {
        fl.trace(item.name + " (" + item.itemType + ")");
    }
} else {
    fl.trace("Library is empty");
}

If you wanted to sort the items in alphabetical order, you need to call the sort() method on the items array and pass a custom sort function, as seen in the following example:

// JSFL
var items = fl.getDocumentDOM().library.items;
items.sort(textCaseInsensitive);
fl.outputPanel.clear();
if (items.length > 0) {
    for each (var item in items) {
        fl.trace(item.name + " (" + item.itemType + ")");
    }
} else {
    fl.trace("Library is empty");
}
 
function textCaseInsensitive(a, b) {
    var aValue = a.name.toLowerCase();
    var bValue = b.name.toLowerCase();
    if (aValue > bValue) {
        return 1;
    } else if (aValue < bValue) {
        return -1;
    } else {
        return 0;
    }
}

This would produce beautifully sorted output similar to the following in your Flash Output panel (depending on what you had in your Flash document’s library):

Button (component)
Component Assets (folder)
Component Assets/_private (folder)
Component Assets/_private/Component_avatar (movie clip)
Component Assets/_private/ComponentShim (compiled clip)
Component Assets/ButtonSkins (folder)
Component Assets/ButtonSkins/Button_disabledSkin (movie clip)
Component Assets/ButtonSkins/Button_downSkin (movie clip)
Component Assets/ButtonSkins/Button_emphasizedSkin (movie clip)
Component Assets/ButtonSkins/Button_overSkin (movie clip)
Component Assets/ButtonSkins/Button_selectedDisabledSkin (movie clip)
Component Assets/ButtonSkins/Button_selectedDownSkin (movie clip)
Component Assets/ButtonSkins/Button_selectedOverSkin (movie clip)
Component Assets/ButtonSkins/Button_selectedUpSkin (movie clip)
Component Assets/ButtonSkins/Button_upSkin (movie clip)
Component Assets/Shared (folder)
Component Assets/Shared/focusRectSkin (movie clip)
FLVPlayback (compiled clip)
Font 1 (font)
Symbol 1 (movie clip)
Symbol 2 (button)
Symbol 3 (graphic)
Symbol 4 (movie clip)
TextLayout (compiled clip)
untitled folder 1 (folder)
Video 1 (video)

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: