It seems to work just fine if selected properties are in the same group, however, it still breaks once removing selected properties from different groups.
doIt();
function doIt(){
var myComp = app.project.activeItem;
var myLayer = myComp.selectedLayers[0];
var propertyCollection = myLayer.selectedProperties;
removeSelectedProperties(propertyCollection)
}
function getSelectedRemovableAncestor(p){
p=p.parentProperty;
while(p && p.parentProperty && (!p.selected || p.parentProperty.propertyType===PropertyType.NAMED_GROUP)) p=p.parentProperty;
return p && p.propertyDepth>=2 ? p : null;
};
function removeSelectedProperties(props){
// Assumes that props is an array sorted in a similar fashion as theLayer/theComp.selectedProperties
// Wont work for an arbitrary array of properties (pre-sorting required)
var N=props.length, n;
var last, parent, temp, indices;
var siblingsToRemove = [];
app.beginUndoGroup("Remove");
while(N>0){
// skip children of named groups, they cant be removed individually
while( (last=props[--N]) && last.parentProperty.propertyType !== PropertyType.INDEXED_GROUP);
if (!last) break;
// last is the child of an indexed group
// see if last has a selected removable parent, in which case, jump to that parent; (can only happen in shapes/text animators/trackers)
while(temp = getSelectedRemovableAncestor(last)){
last=temp.parentProperty(temp.propertyIndex);
// check the new index of last in the props array;
while (props[--N]!=last);
};
// last and the selected children of its parent can be removed:
parent = last.parentProperty;
indices = [last.propertyIndex];
while(N>0 && props[N-1].parentProperty == parent) indices.push(props[--N].propertyIndex);
// normally, indices is already sorted in descending order, eg [10,8,6,2,1]
// indices.sort(function(a,b){return b-a;});
siblingsToRemove.push({parent: parent, indices:indices});
};
for (n=0; n<siblingsToRemove.length; n++){
parent = siblingsToRemove.parent;
indices = siblingsToRemove.indices;
for (i=0; i<indices.length; i++) parent.property(indices).remove();
};
app.endUndoGroup();
return;
};