If the script creates similar objects every time you run it, then it becomes difficult to navigate in the Layers panel. For further work, the user will have to deal with it.
How to get rid of chaos:
- Create a new layer for objects. But this may disrupt the user’s layer system
- Group the created objects on the current layer.
Example:
var newGroup = addGroup('MARKERS');
for (var i = 0; i < selection.length; i++) {
selection[i].move(newGroup, ElementPlacement.PLACEATBEGINNING);
}
function addGroup(name) {
var lblGroup;
try {
lblGroup = activeDocument.groupItems.getByName(name);
} catch (e) {
lblGroup = activeDocument.activeLayer.groupItems.add();
lblGroup.name = name;
}
return lblGroup;
}
The try...catch
construct is needed so that when you run the script again, you don’t possibly duplicate an already existing group.