Copy link to clipboard
Copied
Hi, is there a quick way how to renumber or batch rename all layers in a file so they would be named in consequent numbers? Doesn't have to start from exact number, I was wondering if maybe there is some sort of script that would help me with that? Thanks Pavel
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
for (var i = 0; i < layers.length; i++) {
layers.name = layers.length - i;
}
}
Copy link to clipboard
Copied
This is straight top level layers only…
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
for (var i = 0; i < layers.length; i++) {
layers.name = i + 1;
}
}
Copy link to clipboard
Copied
Hi Mark,
thanks a lot for your answer, it worked perfectly... just one minor tweaking, would it be possible to start the numbering from the most bottom layer and continue upwards?
Thanks again
Pavel
Copy link to clipboard
Copied
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
for (var i = 0; i < layers.length; i++) {
layers.name = layers.length - i;
}
}
Copy link to clipboard
Copied
Thanks a lot, Mark!
Copy link to clipboard
Copied
Thank you for the script. It is very useful.
Could you help me with one thing? If I want to add letters "ca" in front of the number, where should I add "ca" and how to do that?
The present script is 1, 2, 3, 4... on each layer.
I want the script to be ca1, ca2, ca3, ca4... on each layer.
Please help me out.
Copy link to clipboard
Copied
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
for (var i = 0; i < layers.length; i++) {
layers.name = 'ca' + (layers.length - i).toString();
}
}
Copy link to clipboard
Copied
I have a script that exports each top layer to a JPG, while all layers named ALWAYS are combined with it (for webdesign screens, the ALWAYS layer usually contains a browser background image, main menu, and other recurring elements).
http://supermetricity.com/2009/02/11/save-layers-as-jpgs-from-illustrator/
I've been trying to combine the rename script with my export script, but with little success.
Ideally, the export script would name the jpgs it makes from the top layers from the bottom up, adding numbers "01" +n befroe the original layer name, and the layer(s) called ALWAYS would not be exported as single JPG at all...
if that's too complex, is there a way to just not rename any layers called ALWAYS in your rename script?
Copy link to clipboard
Copied
Not too sure if this is what you meant or if this is the best way but I tried…
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
var myNumber = 1;
for (var i = layers.length-1; i >= 0; i--) {
if (layers.name != 'ALWAYS') {
layers.name = myNumber + 'n ' + layers.name;
myNumber = myNumber + 1;
continue;
}
}
myNumber = 1;
}
Should number from bottom up skipping layers names 'ALWAYS'
Copy link to clipboard
Copied
fantastic, thanks so much!
that's what I actually tweaked it to now:
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
var myNumber = 1;
for (var i = layers.length-1; i >= 0; i--) {
if (layers.name != 'ALWAYS') {
layers.name = myNumber + '-' + activeDocument.name;
myNumber = myNumber + 1;
continue;
}
}
myNumber = 1;
}
Copy link to clipboard
Copied
hmmmm. sorry to bother again — is there any way to add a leading zero to the ones below ten, i.e. creating a double digit number?
thanks a million
m
Copy link to clipboard
Copied
docRef is a reference to the activeDocument so while we are inside of the with (docRef) {doStuffHere} you don't need to mention it again… Just use 'name'. This should check the layers length as string to count the required zero padding…
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
var myNumber = 1;
for (var i = layers.length-1; i >= 0; i--) {
var x = layers.length.toString().length;
var z = zeroPad(myNumber, x);
if (layers.name != 'ALWAYS') {
layers.name = z + '-' + name;
myNumber = myNumber + 1;
continue;
}
}
// myNumber = 1;
}
function zeroPad(num, digit) {
var tmp = num.toString();
while (tmp.length < digit) {tmp = '0' + tmp;}
return tmp;
}
Copy link to clipboard
Copied
Thanks Mark!
works perfect now, I had to make a small adjustment though, and set digit to 2...see below:
#target illustratorvar docRef = app.activeDocument;
with (docRef) {Copy link to clipboard
Copied
Then you don't need this line… 'var x = layers.length.toString().length;' You have hardcoded the length so you won't get 001, 011, 123 if the number of layers increases beyond 99.
Copy link to clipboard
Copied
Then you don't need this line… 'var x = layers.length.toString().length;' You have hardcoded the length so you won't get 001, 011, 123 if the number of layers increases beyond 99.
Copy link to clipboard
Copied
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
for (var i = 0; i < layers.length; i++) {
layers.name = 'INPUTFROMUSER' + (layers.length - i).toString();
}
}
wow thanks, that really helped me out..
but i was wondering there must be a way to get input dialogue window so user can insert prefix each time the script is run. currently i have to change it each time from notepad when script is run.
I am not a programmer, but in python there is a command "input" and "raw_input", value is stored in this variable and later that variable is used.
can somebody help me out?
Copy link to clipboard
Copied
Hi! Hopefully this suggestion will solve your issue.
use a simple prompt:
#target illustrator
function test(){
var input = prompt("Enter info", "");
if(input){
alert(input);
}
};
test();
Copy link to clipboard
Copied
thanks, combining with previous version it worked
#target illustrator
var input = prompt("Enter Layer Name", "");
var docRef = app.activeDocument;
with (docRef) {
for (var i = 0; i < layers.length; i++) {
layers.name = input + (layers.length - i).toString();
}
}
alert("Layers renamed");
Copy link to clipboard
Copied
thanks to everyone for the help so far. these scripts are very helpful.
is there any way that i can limit the script to selected layers only?
Copy link to clipboard
Copied
No, the selection state of layers is not exposed to the Scripting model.
Copy link to clipboard
Copied
But the visibility of Layers is. You could try turning off the Layers you don't want and then selecting the ones still visible.
Copy link to clipboard
Copied
Ooh, that could work Larry. I'm tied up with other work so I can't do too much research right now.
How would I address visible layers only? Would it be in this initial statement?
Instead of:
var docRef = app.activeDocument;
It might be:
var docRef = app.activeDocument.visibility.true;
(im sure that syntax is not correct for visibility -- any advice on that attribute would be much appreciated)
thanks in advance!
Copy link to clipboard
Copied
You would probably need to reference the layers property of the document.
Copy link to clipboard
Copied
Kevin,
Your guess would only test if the active document is visible But at least you got the right property name!
What Larry says; you need to test "visible" per layer. I don't have the opportunity to test it right now, but here is the last script from this thread, with a "visible" test blatantly injected where it seemed appropriate:
(I see a couple of odd programming 'choices' in this script but it might be because it evolved throughout the discussion -- a total rewrite would oblige me to test it before posting ...)
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
var myNumber = 1;
for (var i = layers.length-1; i >= 0; i--) {
if (layers.visible == true) // <--- Testing .. 1,2,3.
{
var x = layers.length.toString().length;
var z = zeroPad(myNumber);
if (layers.name != 'ALWAYS') {
if (layers.name != 'GRID') {
layers.name = z + '-' + name;
myNumber = myNumber + 1;
continue;
}
}
}
}
// myNumber = 1;
}
function zeroPad(num) {
var tmp = num.toString();
while (tmp.length < 2) {tmp = '0' + tmp;}
return tmp;
}
Copy link to clipboard
Copied
Jongware, that is totally doing what I want it to do.
Thanks so much to you, Larry and everybody else !!!!