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

Background layer to file name script.

Community Beginner ,
Apr 05, 2018 Apr 05, 2018

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

}

TOPICS
Actions and scripting

Views

2.5K

Translate

Translate

Report

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;

Votes

Translate

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

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;

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Thanks! I appreciate it!

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

If you do, mark his reply as correct solution

Votes

Translate

Translate

Report

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

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!

Votes

Translate

Translate

Report

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

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.

Votes

Translate

Translate

Report

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

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

 

Votes

Translate

Translate

Report

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

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;

Votes

Translate

Translate

Report

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

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

 

Votes

Translate

Translate

Report

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

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 😄

Votes

Translate

Translate

Report

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

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:

 

regexr.png

 

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Amazing Information, thank you very much 😄

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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

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:

 

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...

 

Votes

Translate

Translate

Report

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