Skip to main content
Known Participant
June 12, 2022
Answered

Changing names of the layers by script

  • June 12, 2022
  • 4 replies
  • 964 views

Dear people,

Perhaps you know how should look like the script changing thae names of all layers to random ones?

 

When I export a GeoPDF the software wants from me the names of the layers should be different from each other. For now I have someting like "Layer 1, Layer 1, Layer 1..."

 

It can  be even such names as "hjbjhgdsdklahf". Completely different characters, but random. Just every layer needs to have different name from the others.

 

When it is not so many of them, I can change them manually, but when there is more layers...

 

Thank you for your thoughts,

This topic has been closed for replies.
Correct answer femkeblanco

 

var layers = app.activeDocument.layers;
for (var i = 0; i < layers.length; i++) {
    var string1 = "";
    for (var j = 0; j < 10; j++) {
        var n = Math.floor(Math.random() * 24) + 97;
        string1 += String.fromCharCode(n);
    }
    layers[i].name = string1;
}

 

4 replies

Participating Frequently
June 12, 2022

I am not familar with GeoPDF, so I don't know if this will be pertanent or not. There is a Preference under Units, to "Identify Object By" and if you choose XML ID each layer becomes unique. Just not sure how it works when saving as or exporting as PDF.

femkeblanco
femkeblancoCorrect answer
Legend
June 12, 2022

 

var layers = app.activeDocument.layers;
for (var i = 0; i < layers.length; i++) {
    var string1 = "";
    for (var j = 0; j < 10; j++) {
        var n = Math.floor(Math.random() * 24) + 97;
        string1 += String.fromCharCode(n);
    }
    layers[i].name = string1;
}

 

Charu Rajput
Community Expert
Community Expert
June 12, 2022

Hello,

You can try the following script that will rename layers to Layer 1, Layer 2, Layer 3 and so on.

var _doc = app.documents[0];
var _layers = _doc.layers
for (var l = 0; l < _layers.length; l++) {
    _layers[l].name = 'Layer ' + (l + 1);
}

 

Note: It will not rename the sublayers.

Best regards
BeffAuthor
Known Participant
June 17, 2022

Thank you guys, all of you, for help.

Works like a gold.

No need to worry about sublayers.

Kurt Gold
Community Expert
Community Expert
June 12, 2022

There are already scripts that can do it. For example, I'd recommend "RenameItems", provided by Sergey Osokin.

 

RenameItems