Not yet rated

Problem

Most Fireworks extensions act on the currently selected elements. Users will expect the same elements to be selected after the command is done, but the selection typically changes during the execution of the command.

Solution

To maintain the selection, all you have to do is copy the current selection into an array and then restore it at the end of the command.

Detailed explanation

    // make a copy of the current selection by concatenating it
    // with an empty array
var originalSelection = [].concat(fw.selection);
 
for (var i = 0, len = originalSelection.length; i < len; i++) {
    // change the selection as needed
}
 
    // restore the original copy of the selection
fw.selection = originalSelection;

Note that some Fireworks API methods, such as dom.removeTransformation(), will cause the reference to the selected element to go stale, so the element will not be selected at the end of the command.  You might also get an error.  To avoid this, reference the affected element again in the originalSelection array at the end of the for loop, e.g.:

originalSelection[i] = fw.selection[0];

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes