Skip to main content
Inspiring
September 26, 2024
Question

Text layer duplicate and update layer name

  • September 26, 2024
  • 2 replies
  • 299 views

So I've been having problems with duplicating text layers via scripts. In that the layer names are not automatically updating when the text content is changed.

 

  if (app.activeDocument.activeLayer.kind == LayerKind.TEXT)
  {
    // duplicate text
    var idCpTL = charIDToTypeID( "CpTL" );
    executeAction( idCpTL, undefined, DialogModes.NO );
  }
  else
  {
    //. .. duplicate layer in prefered way...

  }

 

Just thought I'd share the joy.

This topic has been closed for replies.

2 replies

Stephen Marsh
Community Expert
Community Expert
September 27, 2024
Inspiring
September 27, 2024

Funily enough I did. The enter, backspace, enter "solution" didn't work for me. So I made my own and thought it worth sharing.

 

 

Legend
September 26, 2024

I wrote this for my own use, because data-driven graphics text layers are just generically named and it was a problem. Apache licensed, feel free to modify it. I strip out return characters in the layerText.

 

/*
Utility Pack Scripts created by David M. Converse ©2018-24

This script renames text layers in an open Photoshop document

Last modifed 7/5/2024

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target photoshop

renameTextLayers();

function renameTextLayers(){
    if(!app.documents.length > 0){
        return;
        }
    var docRef = null;
    var vis = true;
    var LayerRef = null;
    var TextRef = null;
    var layerText = '';
    try{
        docRef = app.activeDocument;
        docRef.activeLayer = docRef.artLayers[0];
        for(var i = 0; i < docRef.artLayers.length; i++){
            LayerRef = docRef.artLayers[i];
            vis = true;
            if(LayerRef.kind == LayerKind.TEXT){
                if(!LayerRef.visible){
                    vis = false;
                    }
                TextRef = LayerRef.textItem;
                layerText = TextRef.contents;
                layerText = layerText.replace(/\r$/, '');
                layerText = layerText.replace(/,\r/, ', ');
                if(layerText.search('\r') == -1){
                    layerText = layerText.split(' ');
                    layerText[0] = layerText[0].replace(',', '');
                    }
                else{
                    layerText = layerText.split('\r');
                    }
                LayerRef.name = layerText[0];
                if(vis == false){
                    LayerRef.visible = false;
                    }
                }
            }
        }
    catch(e){
        Window.alert(e + e.line);
        }
    }