Copy link to clipboard
Copied
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.
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);
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Myra — Droplets look interesting. I wasn't aware of their existence. Thanks for that.
Stephen.
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Hi and thanks very much for the link to your script. I'll try it and see how I get on.
Cheers,
Stephen.
Copy link to clipboard
Copied
Sorry - I just noticed your question about which file format I wanted to save as. It is PDF for now.
Cheers,
Stephen.
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
@pixxxelschubser Perfect! Thank you very much. Yes it does just what I was trying to do.
(First time I've used a script in PS — I had a bit of figuring out to do...)
Cheers,
Stephen.
Copy link to clipboard
Copied
It could be that this will also help you:
Copy link to clipboard
Copied
Yes — that looks useful.
I have taken a copy.
Thanks again,
Stephen.
Copy link to clipboard
Copied
@stephenmcateer – as you mentioned not being a programmer, do let the forum know if you have problems putting the various pdfSaveOptions into the script.
There is an example in a full script context that you can use for reference here:
Copy link to clipboard
Copied
Info on saving/installing/running scripts here:
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
One should also note that there are various illegal filename characters, which are valid for layer names.
Edit: This updated code is very restrictive only allowing alpha/numeric, hyphen, underscore, period and word space:
var removeExtension = activeDocument.layers[0].name.replace(/\.[^\.]+$/, '');
var cleanPeriodChars = removeExtension.replace(/\./g, "");
var cleanLayerName = cleanPeriodChars.replace(/[^-_0-9a-zA-Z\. ]/g, "_");
cleanLayerName = cleanLayerName.replace(/_+/g, "_");
var saveFile = File("~/Desktop/" + cleanLayerName + ".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);
Copy link to clipboard
Copied
Thanks for both comments Stephen.
I figured out how to save and run a script, though I'm still bit unsure how the Script Events Manager works. I'll look into that a bit more tomorrow.
Thanks also for the sanitising script — should be useful.
I have a further requirement for a script which will be more complicated but it's not urgent. I'll maybe post what I need on here later. (I tried to have ChatGPT create a script to do what I need - it did generate a script but it didn't work properly.)
Anyway,
Cheers,
Stephen.
Copy link to clipboard
Copied
I'll need to update the code, there is a problem with the sanitising...
Code updated!
As for the Script Events Manager:
https://prepression.blogspot.com/2021/10/photoshop-script-events-manager.html
Copy link to clipboard
Copied
Thanks again Stephen. The guide looks handy.
I'll look at it in more detail tomorrow - it's after midnight here.
Cheers,
Stephen.