Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

Script to rename layer from text in the layer.

New Here ,
Jun 14, 2023 Jun 14, 2023

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?

TOPICS
How-to , Scripting
573
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 14, 2023 Jun 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

...
Translate
Adobe
Community Expert ,
Jun 14, 2023 Jun 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 14, 2023 Jun 14, 2023
LATEST

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
RenameLayerAsText.gif

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines