Copy link to clipboard
Copied
Hello!
I'd like to create a script to rename my background layer to the document filename without the extension.
This script works but it creates a text layer instead of converting the background layer to layer with document name.
So I could use some help in knowing what I have to change to make it work.
Let me know!
Thanks!
// this script is a variation of the script addTimeStamp.js that is installed with PH7
if ( documents.length > 0 )
{
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
try
{
var docRef = activeDocument;
// Now create a text layer at the front
var myLayerRef = docRef.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "Filename";
var myTextRef = myLayerRef.textItem;
// strip the extension off
var fileNameNoExtension = docRef.name;
fileNameNoExtension = fileNameNoExtension.split( "." );
if ( fileNameNoExtension.length > 1 ) {
fileNameNoExtension.length--;
}
fileNameNoExtension = fileNameNoExtension.join(".");
myTextRef.contents = fileNameNoExtension;
// off set the text to be in the middle
myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
myTextRef.size = 20;
}
catch( e )
{
// An error occurred. Restore ruler units, then propagate the error back
// to the user
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
throw e;
}
// Everything went Ok. Restore ruler units
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
}
else
{
alert( "You must have a document open to add the filename!" );
}
1 Correct answer
var n = app.activeDocument.name.lastIndexOf(".");
app.activeDocument.backgroundLayer.name = n>=0?app.activeDocument.name.substr(0, n):app.activeDocument.name;
Explore related tutorials & articles
Copy link to clipboard
Copied
var n = app.activeDocument.name.lastIndexOf(".");
app.activeDocument.backgroundLayer.name = n>=0?app.activeDocument.name.substr(0, n):app.activeDocument.name;
Copy link to clipboard
Copied
Thanks! I appreciate it!
Copy link to clipboard
Copied
If you do, mark his reply as correct solution
Copy link to clipboard
Copied
The script to use filename to rename the background layer no longer works in Photoshop cc2019.
Anybody know how to make it work in new version?
Thanks!
Copy link to clipboard
Copied
I can tell is true, is not working anymore, I hope anyone know a new code to rename current layer to file name without the extension, thank you for anyone who can help.
Copy link to clipboard
Copied
You can use this more generic code, r-bin's was specifically for the Background layer and still works as originally intended:
app.activeDocument.activeLayer.name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
Copy link to clipboard
Copied
Hello, thank you very much, it work!,
I Also just used this one in case it help someone:
var srcDoc = app.activeDocument; // to call the source document
var docName = app.activeDocument.name; // Get the name of the document
docName = docName.slice(0, -4); // Trim the extension, last four characters (ie ".psd")
srcDoc.activeLayer.name = docName;
Copy link to clipboard
Copied
Just keep in mind that the JS slice in your example is fixed to four characters from the end of the text string:
.slice(0, -4);
While a regular expression can gracefully handle the dot . and any length extension:
.replace(/\.[^\.]+$/, '');
Copy link to clipboard
Copied
You are tottally right, and is more elegant, only problem is regex still very difficult to understand to me but I am learning, thank you for be so kind 😄 have a blessed day 😄
Copy link to clipboard
Copied
Websites such as regexr or regex101 can help to explain regular expressions, such as this screenshot of the common filename extension pattern:
In other words, find anything that begins with a dot, followed by any characters that are not a dot one or more times, working backwards from the end/far right of the text string.
Copy link to clipboard
Copied
Amazing Information, thank you very much 😄
Copy link to clipboard
Copied
I tried the code in Photoshop 2022 and it still works.
Copy link to clipboard
Copied
Yes you are right, was something wrong in my end.
Copy link to clipboard
Copied
Some related topics where the Script Events Manager is used to automatically rename the Background layer to the doc name when an image is opened into Photoshop:

