Skip to main content
Known Participant
October 9, 2019
Answered

Script to Copy Active Layer name to a "Label" text layer and filter out numbers in the text

  • October 9, 2019
  • 4 replies
  • 2714 views

I am trying to create or find a script that basically detects the current active layer and copy's the layer name and then  Creates/Updates a text layer named "Label" with the content being the text filtering out any numbers and trailing spaces. 

I hope that makes sense. 

Secondly I am also wanting a second script that will allow me to save the current visible layers as a jpg named after the active layer name.  

This topic has been closed for replies.
Correct answer Stephen Marsh
for reference we are working on the "layer name to text" script, ok so for an example jason carroll-01 is the layer name, I want to strip the hypen at the end and the numbers out and insert a carriage return in the first space. this will correctly enter the text into the text layer

OK, try this:

 

var myLayerName = myDoc.activeLayer.name.replace(/ {2,}|-|\d+| +$|^ +/g, '').replace(/ +/g, ' ').replace(/ /, '\r');

4 replies

Geppetto Luis
Legend
October 10, 2019

A question
if the level name has numbers
these are not reproduced

you can enter it in code for this

Chuck Uebele
Community Expert
Community Expert
October 9, 2019

This will save a file to the same folder as the original file, and use the name of the current selected layer.

#target photoshop
var doc = activeDocument;
var curLayer = doc.activeLayer;

saveFile ()

function saveFile(){
       var idsave = charIDToTypeID( "save" );
        var desc626 = new ActionDescriptor();
        var idAs = charIDToTypeID( "As  " );
            var desc627 = new ActionDescriptor();
            var idEQlt = charIDToTypeID( "EQlt" );
            desc627.putInteger( idEQlt, 8 );
            var idMttC = charIDToTypeID( "MttC" );
            var idMttC = charIDToTypeID( "MttC" );
            var idNone = charIDToTypeID( "None" );
            desc627.putEnumerated( idMttC, idMttC, idNone );
        var idJPEG = charIDToTypeID( "JPEG" );
        desc626.putObject( idAs, idJPEG, desc627 );
        var idIn = charIDToTypeID( "In  " );
        desc626.putPath( idIn, new File( doc.path +'/'+curLayer.name+'.jpg' ) );
        var idDocI = charIDToTypeID( "DocI" );
        desc626.putInteger( idDocI, 221 );
        var idCpy = charIDToTypeID( "Cpy " );
        desc626.putBoolean( idCpy, true );
        var idsaveStage = stringIDToTypeID( "saveStage" );
        var idsaveStageType = stringIDToTypeID( "saveStageType" );
        var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
        desc626.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
    executeAction( idsave, desc626, DialogModes.NO );
    }

 

 

Known Participant
October 9, 2019
This worked as well! thanks Chuck! You are a real genius!
Chuck Uebele
Community Expert
Community Expert
October 9, 2019

Stephen, pretty good, but the OP wanted also stated that the label text layer could or could not exists, so you need to add some code to check if that layer does exist:

#target photoshop
var myDoc = app.activeDocument;
var myRulers = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var myLayerName = myDoc.activeLayer.name.replace(/\d+|^ +| +$/g, '').replace(/ +/g, ' ');
try{
    var myLayerText = myDoc.layers.getByName('Label');
    var myText = myLayerText.textItem;
    }
catch(e){
    var myLayerText = myDoc.artLayers.add();
    myLayerText.kind = LayerKind.TEXT;
    var myText = myLayerText.textItem;
    myColor = new SolidColor;
    myColor.rgb.red = 255;
    myColor.rgb.green = 0;
    myColor.rgb.blue = 0;
    myLayerText.textItem.color = myColor;
    myText.position = [10,20]; // Upper Left
    myText.justification = Justification.LEFT;
    myText.font = 'Courier';
    myText.size = 30;
    
    myLayerText.name = 'Label';
    }
finally{
    myText.contents = myLayerName;
    }
app.preferences.rulerUnits = myRulers;
Known Participant
October 9, 2019
@chuck this is excellent! and seems to work perfectly! I did comment out the color and position and it updates the layer and keeps the position and style. Thanks so much!
Stephen Marsh
Community Expert
Community Expert
October 9, 2019

I am pretty sure that this is not 100% what you are looking for as more info is required, however it is a start. It does not check for an existing text layer titled "Label" and will create a new one (I'm new to scripting so this is something that I'll need to look into deeper, perhaps an if/else)...

 

 

 

#target photoshop
var myDoc = app.activeDocument;
var myRulers = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var myLayerName = myDoc.activeLayer.name.replace(/\d+|^ +| +$/g, '').replace(/ +/g, ' ');
var myLayerText = myDoc.artLayers.add();
myLayerText.kind = LayerKind.TEXT;
var myText = myLayerText.textItem;
myColor = new SolidColor;
myColor.rgb.red = 255;
myColor.rgb.green = 0;
myColor.rgb.blue = 0;
myLayerText.textItem.color = myColor;
myText.position = [10,20]; // Upper Left
myText.justification = Justification.LEFT;
myText.font = 'Courier';
myText.size = 12;
myText.contents = myLayerName;
myLayerText.name = 'Label';
app.preferences.rulerUnits = myRulers;