var dom = fw.getDocumentDOM(); // document object var sel = new Array().concat(fw.selection); // saved selection function AddToGroup() { if(!sel.length) return false; // find selected group(s) and regular elements var groups = [], elements = []; for(var s in sel){ if(String(sel[s])=='[object Group]'){ groups.push(sel[s]); }else{ elements.push(sel[s]); } } if(!groups.length) { alert('Select at least one group to add into.'); return false; } // the elements to merge into the top selected group, includes any other groups var merge = elements.concat(groups.slice(1)); // select and temporarily group the merge elements fw.selection = merge; dom.group(); fw.selection[0].name = 'AddToGroup_temporaryMergeGroup'; // cut merge elements dom.clipCut(); // select element in target group, ready to paste into fw.selection = [groups[0].elements[0]]; // temporarily group it to protect it in case it has a mask, since we will use PasteAsMask dom.group(); fw.selection[0].name = 'PasteInGroup_temporaryMaskGroup'; // here is the main event: PasteAsMask gets the old elements into the group dom.clipPasteAsMask("ask user", "vector", "add"); // now ungroup the Mask group dom.ungroup(); // find the temporary groups and ungroup them fw.selection = dom.findNamedElements('PasteInGroup_temporaryMaskGroup'); dom.ungroup(); fw.selection = dom.findNamedElements('AddToGroup_temporaryMergeGroup'); dom.ungroup(); // select the parent group and we are done dom.selectParents(); return true; } AddToGroup();