Skip to main content
stephenmcateer
Known Participant
January 14, 2023
Answered

Use Layer Name As File Name When Saving - Is this Possible Using a Script?

  • January 14, 2023
  • 3 replies
  • 5073 views

Is it possible to have Photoshop use the name of a layer to name the file when saving the image? [See attached image forwhat I mean by layer name.]

 

Could I then have Photoshop run this script at the end of an Action that I've built?

 

[I think this might be possible using a script but I'm not a programmer.]

 

Thanks for any info.

 

Stephen.

 

Correct answer pixxxelschubser

Does this code snippet work for you?

 

var saveFile = File("~/Desktop/"+app.activeDocument.layers[0].name+".pdf")

pdfSaveOptions = new PDFSaveOptions();
// You should still set the desired PDF save options - but for test purposes it also works without them
activeDocument.saveAs(saveFile, pdfSaveOptions); 

 

 

3 replies

Legend
January 14, 2023

In what image format did you plan to save the layers?

 

You can use my Batch for Layers script, I just created it for such tasks. However, it supports only saving in JPG, PSD, TIFF

(script saves layers in the same directory from where the source file was opened)

stephenmcateer
Known Participant
January 14, 2023

Hi and thanks very much for the link to your script. I'll try it and see how I get on.

 

Cheers,

Stephen.

pixxxelschubser
Community Expert
Community Expert
January 14, 2023

Can you please define this very precisely: ".... when saving the image ...".


Yes, you can read the name of the active layer via script and use it later in the script.

 

alert (app.activeDocument.activeLayer.name)

 

stephenmcateer
Known Participant
January 14, 2023

Hi,

 

Thanks for that code snippet.

 

What I'm trying to do is automate the Save process so that I don't have to manually enter a filename each time — I will be creating hundreds or maybe thousands of files.

 

Process:

====

Once I've finished editing the PSD, I have an Action that saves the file as PDF. It stops to allow me to type or paste in a  filename. 

 

The top layer in the PSD inherits its Layer Name from the original filename in the Photoshop Library, since I'm dragging the image onto a new PSD Layer from there.

 

At the moment, I can manually copy that Layer Name and manually paste it into the File name box when saving the file. 

 

Being able to do this programmatically would save lots of time.

 

Thanks again.

 

Stpehen.

Myra Ferguson
Community Expert
Community Expert
January 14, 2023

You can generate files from your layers using the layer names (and set other parameters) when you use File > Generate > Image Assets and name the layers with an extension (and additional parameters as needed). Here's more information on how you would name the layers and what else you can do with generating the assets:

After you generate the assets, you could run the action either on a batch or create a droplet and drag the generated assets to the droplet to run the action:

 

stephenmcateer
Known Participant
January 14, 2023

Thanks Myra.

 

This looks usefu but I think I've hit a bug in Photoshop — when I try to run the Generate Image Assets option, nothing happens. I'm on Version 24.0.1. (I see reports of this bug from a few versions ago. Maybe it hasn't been fixed…)

 

Cheers,

Stephen.