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

Background layer to file name script.

Community Beginner ,
Apr 05, 2018 Apr 05, 2018

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!" );

}

TOPICS
Actions and scripting
2.9K
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

People's Champ , Apr 05, 2018 Apr 05, 2018

var n = app.activeDocument.name.lastIndexOf(".");

app.activeDocument.backgroundLayer.name = n>=0?app.activeDocument.name.substr(0, n):app.activeDocument.name;

Translate
Adobe
People's Champ ,
Apr 05, 2018 Apr 05, 2018

var n = app.activeDocument.name.lastIndexOf(".");

app.activeDocument.backgroundLayer.name = n>=0?app.activeDocument.name.substr(0, n):app.activeDocument.name;

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
Community Beginner ,
Apr 05, 2018 Apr 05, 2018

Thanks! I appreciate it!

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
LEGEND ,
Apr 05, 2018 Apr 05, 2018

If you do, mark his reply as correct solution

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
Community Beginner ,
Oct 25, 2018 Oct 25, 2018

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!

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
Participant ,
Jan 20, 2022 Jan 20, 2022

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.

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
Community Expert ,
Jan 20, 2022 Jan 20, 2022

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(/\.[^\.]+$/, '');

 

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
Participant ,
Jan 20, 2022 Jan 20, 2022

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;

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
Community Expert ,
Jan 20, 2022 Jan 20, 2022

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(/\.[^\.]+$/, '');

 

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
Participant ,
Jan 20, 2022 Jan 20, 2022

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 😄

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
Community Expert ,
Jan 21, 2022 Jan 21, 2022

 

Websites such as regexr or regex101 can help to explain regular expressions, such as this screenshot of the common filename extension pattern:

 

regexr.pngexpand image

 

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.

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
Participant ,
Jan 21, 2022 Jan 21, 2022

Amazing Information, thank you very much 😄

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
LEGEND ,
Jan 21, 2022 Jan 21, 2022

I tried the code in Photoshop 2022 and it still works.

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
Participant ,
Jan 21, 2022 Jan 21, 2022
LATEST

Yes you are right, was something wrong in my end.

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
Community Expert ,
Jan 21, 2022 Jan 21, 2022

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:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-for-open-as-layer-without-file...

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/change-default-background-layer-to-or...

 

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