Copy link to clipboard
Copied
Hello,
i use this code for replacing names in layers from github (adobe-illustrator-layer-renamer/Rename Layers.jsx at master · seblavoie/adobe-illustrator-layer-rena... )
it works fine but it just renames top level layers. Can somebody explain how I can recurse through layers? My knowledge of javascript is limited
// Generated by CoffeeScript 1.7.1
var LayerRenamer, layerRenamer;
LayerRenamer = (function() {
function LayerRenamer() {
if (app.activeDocument.selection.length > 0) {
this.replacements = [prompt("What would you like to replace?", "Eg: source"), prompt("What would you like to replace it with?", "Eg: replacement")];
this.renameLayers(app.activeDocument.selection);
} else {
alert("Select the layers you would like to be renamed.");
}
}
LayerRenamer.prototype.renameLayers = function(layers) {
var layer, name, _i, _len, _results;
_results = [];
for (_i = 0, _len = layers.length; _i < _len; _i++) {
layer = layers[_i];
layer = layer.parent;
name = layer.name;
_results.push(layer.name = name.replace(this.replacements[0], this.replacements[1]));
}
return _results;
};
return LayerRenamer;
})();
layerRenamer = new LayerRenamer();
Copy link to clipboard
Copied
Try this:
function recurseLayers(parent) {
for (var iLayer = 0; iLayer < parent.length; iLayer++) {
var curLayer = parent[iLayer];
// TODO make all changes on layer
recurseLayers(curLayer.layers);
}
}
recurseLayers(app.activeDocument.layers);
Copy link to clipboard
Copied
thanks for your help! it worked and I learned more about Javascript
Just one question how can I use the same code with the selection-command app.activeDocument.selection. Somehow It won't work with it.
function recurseLayers(p1) { var results, name
results = [];
for (var iLayer = 0; iLayer < p1.length; iLayer++) {
var curLayer = p1[iLayer];
name = p1[iLayer].name;
results.push(p1[iLayer].name = name.replace("copy",""));
recurseLayers(curLayer.layers);
}
return results;
};
recurseLayers(app.activeDocument.layers);
Copy link to clipboard
Copied
As far as i know app.activeDocument.selection doesn't store selected layers.
Selected layer is stored under app.activeDocument.selectedLayer, but it's only one layer.
I'm not able to find, where highlighted layers are stored unfortunatelly, maybe someone else will have an idea.
For now i'll go for some workaround, which will require you to lock/unlock/hide/unhide only layers which you want to rename.
Example below will change names of all visible layers, so you have to hide all layers, which shouldn't be changed.
function recurseLayers(p1) {
var results, name
results = [];
for (var iLayer = 0; iLayer < p1.length; iLayer++) {
var curLayer = p1[iLayer];
if (curLayer.visible == true)
results.push(curLayer.name = curLayer.name.replace("copy", ""));
recurseLayers(curLayer.layers);
}
return results;
};
recurseLayers(app.activeDocument.layers);
Find more inspiration, events, and resources on the new Adobe Community
Explore Now