Skip to main content
Participant
June 14, 2023
Answered

Script to rename layer from text in the layer.

  • June 14, 2023
  • 2 replies
  • 526 views

Hi,

 

I'm looking for a scipt that will change the layer name to the same as the text contained on the layer.

So in this instance it would change Layer 2 into black.

 

Is this possible?

This topic has been closed for replies.
Correct answer Charu Rajput

Hi, you can try the following script

var _layers = app.activeDocument.layers;
for (var l = 0; l < _layers.length; l++) {
    var _textItem = _layers[l].groupItems[0].textFrames[0];
    if (_textItem.name != '') {
        _layers[l].name = _textItem.name;
    }
    else {
        _layers[l].name = _textItem.contents;
    }
}

 

The above script will work assuming your layer structure is same for all. That means, Each layer will have one group and that group will atleast single textframe. If there are multiple textframes inside teh group, it will pickup the first one and use its name if exists.

2 replies

Sergey Osokin
Inspiring
June 15, 2023

I will add my own version. The interface allows you to manage the renaming in a flexible way. The script first collects all text objects in the layer and then takes the first or the last one for the layer name.
https://github.com/creold/illustrator-scripts/blob/master/md/Layer.md#renamelayerastext

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
June 14, 2023

Hi, you can try the following script

var _layers = app.activeDocument.layers;
for (var l = 0; l < _layers.length; l++) {
    var _textItem = _layers[l].groupItems[0].textFrames[0];
    if (_textItem.name != '') {
        _layers[l].name = _textItem.name;
    }
    else {
        _layers[l].name = _textItem.contents;
    }
}

 

The above script will work assuming your layer structure is same for all. That means, Each layer will have one group and that group will atleast single textframe. If there are multiple textframes inside teh group, it will pickup the first one and use its name if exists.

Best regards