Library Item selection and manipulation

Categories:Working with Flash

Library item selection

xJSFL brings a new way to select items in the Flash library, based on CSS-style selection, and inspired by jQuery and other JavaScript libraries. Instead of accessing items via fl.getDocumentDOM().library.items[], you can now use the Item Selector function and supply a selector expression, like so:

var items = $$('game objects/backgrounds/*');

The expression above will return all items in the game objects/backgrounds/ folder of the current document's library, as an ItemCollection instance, which is discussed in a moment.

Note that the function doesn't select the items in the library -- it merely returns a reference to them. You do not need to have items selected in order to modify their properties.

The following basic selector types are supported (for the full list, see here):

name of item
/path/to/item
:type
:pseudo
.Class
.package
[attribute]
* (wildcards)
{min|max} (ranges)
comma,separated,rules

Selectors can also be combined, to make a selection more specific:

assets/player/:graphic
#*Skin:movieclip
#*Skin:movieclip[linkageExportInFirstFrame=false]

For the full lowdown on Item Selection functionality the Item Selector function page.

The ItemCollection class

The results of an item selection return an instance of the ItemCollection class, providing LibraryItem-specific Collection functionality.

The ItemCollection class currently has around 10 methods, including attr(), move(), remove(), and exec().

By combining the Item Selector function with ItemCollection functionality, you're now able to things like smooth all bitmaps in the library with one simple line of code:

$$(':bitmap').attr('allowSmoothing', true);

exec() is another useful method that allows you to open each item for editing, then run a callback function on the contents of each:

// callback function
    function addStopFrame()
    {
        var context = Context.create();
        context.frame.actionScript = 'stop();';
    }

// process all movieclips in the library
    $$(':movieclip').exec(addStopFrame);

For full information about ItemCollection's functionality see the API reference.

Running scripts interactively, on selected library items

While the exec() method above is a useful function, sometimes you may want to batch-process multiple library items with a single JSFL script.

For example, you might be developing a script on a single library item, perfecting the right JSFL to select layers, rename them, modify objects etc., then run the same script on 100 similar items in the library.

From Komodo Edit, you can do this at a keystroke, using the keyboard shortcut combo CTRL+SHIFT+ALT+Enter. Komodo will save the current script, then instruct Flash to loop over the selected library items, editing each on in turn and executing your just-saved code.

This is a quick, interactive way to update a lot of items at the same time.

Next Steps

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may 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>