Skip to main content
Participant
November 1, 2023
Answered

Requesting Help Updating Simple Script

  • November 1, 2023
  • 2 replies
  • 389 views

I'm an accomplished action writer but just can't see the girl in the red dress yet when it comes to scripting. I have a java script that an old coworker, Adam Kobrin, ( thanks again Adam! ) wrote that captures the active psd's file name and renames the active layer that name. I'm hoping that someone out there in the Acosystem can help me by adding a variable in the script that will be appended to file name on the layer.  My intent would be to modify the scripts variable as needed before running.  I will paste script below:

 

/*************************************************************
 
  Rename layer with filename (no extension).
  
  This makes the "product" Smart Object have a non-generic name.
 
**************************************************************/
 
 
 
var doc = app.activeDocument;
 
//remove extension and rename layer
if (doc.name.indexOf(".") !== -1) {
  var noExt = doc.name.replace(/.([^.]*)$/, ""); //remove last dot and after
doc.activeLayer.name = noExt;
 
}
This topic has been closed for replies.
Correct answer Lumigraphics

Append means add to the end, so presumably you would just need:

var x = 'my value';
doc.activeLayer.name = noExt + x;

2 replies

LumigraphicsCorrect answer
Legend
November 2, 2023

Append means add to the end, so presumably you would just need:

var x = 'my value';
doc.activeLayer.name = noExt + x;
Stephen Marsh
Community Expert
Community Expert
November 2, 2023

@Lumigraphics 

 

Yes, I added/appended a suffix, however I still think that it's best to clearly set out the requirements.

Legend
November 2, 2023

Not everyone knows the difference between append and prepend. 😉

Stephen Marsh
Community Expert
Community Expert
November 1, 2023

@See Nelsen – Your post isn't clear to me. As an example, the following alteration will prompt for a suffix and use an underscore separator:

 

/*************************************************************
 
  Rename layer with filename (no extension).
  
  This makes the "product" Smart Object have a non-generic name.
 
**************************************************************/
 
 
 
var doc = app.activeDocument;
 
//remove extension and rename layer
if (doc.name.indexOf(".") !== -1) {
  var noExt = doc.name.replace(/.([^.]*)$/, ""); //remove last dot and after
  var thePrompt = prompt("Enter a Suffix:", "");
doc.activeLayer.name = noExt + "_" + thePrompt;
 
}

 

The suffix could be a prefix instead, or it could be hard-coded without a prompt. The separator could be a space or hyphen... Or no separator?

 

Please clearly explain what you are looking for with a before/after naming example, thanks.