Answered
Script to arrange one pathItem per layer
I am looking for a script that moves all individual items to their own layer using the same layer name.


I am looking for a script that moves all individual items to their own layer using the same layer name.


@nutradial this is one way...
/**
* @file One Item Per Layer 2.js
*
* Moves a layer's page items to a new top-level layer,
* matching properties of the item's original layer.
*
* @author m1b
* @version 2024-12-19
*/
(function () {
// will only target this layer
const TARGET_LAYER = 'Layer 2';
var doc = app.activeDocument,
items = doc.pageItems;
// loop over the page items, backwards,
// moving each to a new layer, which is
// a copy of the original layer
for (var i = items.length - 1, item, layer, newLayer; i >= 0; i--) {
item = items[i];
layer = getLayer(item);
if (
!layer
|| TARGET_LAYER !== layer.name
)
continue;
layer.locked = false;
layer.visible = true;
// create the new layer
newLayer = doc.layers.add();
newLayer.color = layer.color
newLayer.locked = layer.locked
newLayer.name = layer.name
newLayer.preview = layer.preview
newLayer.printable = layer.printable
newLayer.visible = layer.visible
// move into place
newLayer.move(layer, ElementPlacement.PLACEAFTER);
item.move(newLayer, ElementPlacement.PLACEATBEGINNING);
}
// remove empty layers
for (var i = doc.layers.length - 1; i >= 0; i--) {
if (
TARGET_LAYER === doc.layers[i].name
&& 0 === doc.layers[i].pageItems.length
)
doc.layers[i].remove();
}
/**
* Returns an item's layer.
* @param {PageItem} item - an Illustrator PageItem.
* @returns {Layer?}
*/
function getLayer(item) {
var layer = item.parent;
while (
undefined !== layer
&& layer.hasOwnProperty('parent')
&& 'Document' !== layer.parent.constructor.name
)
layer = layer.parent;
if ('Layer' === layer.constructor.name)
return layer;
};
})();Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.