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
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.
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;
}
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) {}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
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.
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.
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;
}
Copy link to clipboard
Copied
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) {}