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

Photoshop Actions - Add File Name

Explorer ,
Jan 17, 2024 Jan 17, 2024

Copy link to clipboard

Copied

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

Views

187

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

Votes

Translate

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;
   }

 

 

Votes

Translate

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) {}

 

 

 

Votes

Translate

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

 

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;
   }

 

 

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

Copy link to clipboard

Copied

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) {}

 

 

 

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