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

Photoshop Actions - Add File Name

Explorer ,
Jan 17, 2024 Jan 17, 2024

Hello All

 

I am hoping that one of you that is more technically adept than myself can answer my question - if so I would be very grateful.

 

I am trying to add the file name as a text layer to a .jpg file.

 

I have downloaded a script (Add File Name.jsx) which does this perfectly.

 

However, I want to remove the .jpg extension, which I have done via an action that I recorded that first runs the above script then edits the text layer to remove the .jpg.

 

This works well the first time, but on subsequent runs, it simply uses the file name that was used when I first recorded the action.

 

I am sure that there is an easy way to achieve this, but I am not competent enough to work out how to access the text layer cretaed by the script rather than when I recorded it.

 

I would be very grateful for any help in this.

 

Thank you in advance

Fred

TOPICS
Actions and scripting
452
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 3 Correct answers

Community Expert , Jan 17, 2024 Jan 17, 2024

@Fred Barrington 


Please post the script code so that it can be modified.

 

Otherwise, try using text find/replace in the action to remove .jpg rather than editing the actual text layer directly.

Translate
LEGEND , Jan 18, 2024 Jan 18, 2024

 

var doc = app.activeDocument;
var x = doc.name;
x = x.split(".");
if(x.length > 1){
   x.length--;
   }
x = x.join(".");
if(doc.activeLayer.kind  == LayerKind.TEXT){
   doc.activeLayer.textItem.contents = x;
   }

 

 

Translate
Community Expert , Jan 18, 2024 Jan 18, 2024

You can use the forum software toolbar icon </> to post code. The code below uses a regular expression to remove the filename extension:

 

#target photoshop
try {
    app.activeDocument.activeLayer.textItem.contents = app.activeDocument.name.replace(/\.[^\.]+$/, '');
} catch (e) {}

 

 

 

Translate
Adobe
Community Expert ,
Jan 17, 2024 Jan 17, 2024
quote

I am trying to add the file name as a text layer to a .jpg file.

By @Fred Barrington

 

JPEGs do not support layers. When you save the JPEG, does it revert to one Background layer called "Background" and remove your filename altogether?

 

Jane

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 17, 2024 Jan 17, 2024

@Fred Barrington 


Please post the script code so that it can be modified.

 

Otherwise, try using text find/replace in the action to remove .jpg rather than editing the actual text layer directly.

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
Explorer ,
Jan 18, 2024 Jan 18, 2024

thank you - I tried attach the script and action, but the file types are not supported. so I am pasting here if that is OK.

Script:

#target photoshop

try{app.activeDocument.activeLayer.textItem.contents = app.activeDocument.name}

catch(e){}

------------

Action: I attach a screenprint as notepad seemed incomprehensible!

BTW

I reralise that jpgs do not save layers, but the action creates the text layer, and the problem is before I save 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
Explorer ,
Jan 18, 2024 Jan 18, 2024

Sorry - I had not realised find/replace works in text layers. I jhave tried this and it works well..

 

Thank you for the suggestion - it is much appreciated.

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 18, 2024 Jan 18, 2024

 

var doc = app.activeDocument;
var x = doc.name;
x = x.split(".");
if(x.length > 1){
   x.length--;
   }
x = x.join(".");
if(doc.activeLayer.kind  == LayerKind.TEXT){
   doc.activeLayer.textItem.contents = x;
   }

 

 

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 18, 2024 Jan 18, 2024
LATEST

You can use the forum software toolbar icon </> to post code. The code below uses a regular expression to remove the filename extension:

 

#target photoshop
try {
    app.activeDocument.activeLayer.textItem.contents = app.activeDocument.name.replace(/\.[^\.]+$/, '');
} catch (e) {}

 

 

 

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