Copy link to clipboard
Copied
Good evening, everyone.
Is there a way to save the active layer in a photoshop document as a .jpeg?
In other words, I would like to:
1. save the current(active) layer as a .jpeg
2. use the active layer name as the file name
3. assign the save path in the script (for example, "/g/MyFolder")
I have few scripts which are close to this but not quite.
Thank you in advance š
I have composites built with individual pictures I want saved out as the layer name. So a background will show up behind each layer of a kids photo.... Is there a way to save the shown layer as a file, or even to automate the process so that layer saves, turns off the layer, turns on the next layer, saves as that layer name, turns off the layer, turns on the next layer,..... But the background won't change and other layers won't change either. Does that make sense?
By @Jenny Rhea
Yes, hav
(It is insane that Adobe doesn't monitor this community to supply direct solutions to questions like this). @gangeek you can use the built-in feature called "Image Assets" under the File > Generate menu in Photoshop. Here is more about the feature, including the ability to export files to specific directories: https://helpx.adobe.com/photoshop/using/generate-assets-layers.html
The user is asking how to save a file in Adobe software using the name of the currently active layer. There are two main methods mentioned:
1. **Using the "Export Layers to Files" option:**
- Hide all other layers except the active one.
- Go to File > Export > Export Layers to Files.
- Check the "Only visible" box and leave the prefix empty.
- This will save the active layer as a separate file with its name.
2. **Using a script:**
- Code snippets are provided in the thread that automate the process of
Copy link to clipboard
Copied
Hello my dear creator,
You want to save the active layer as a .jpeg file right? First of all select the layer you want to save it as a seperate file then right click on the layer > Export as
Select the format and click the Export button.
I hope this will helps you.
Thank you my dear creator.
Copy link to clipboard
Copied
OK, well, it is excellent! I had never known about this way to save a layer to a file - thank you for posting this š
However, I was wondering if there is a way to do it with scripting. I have several scripts which will save current layer to file but none of them are quite what I am after.
I am hoping to just run script, and it will automatically save current layer to file (.jpeg) with layer name as filename and also dictate (in the script) the path in which file will be saved to.
Copy link to clipboard
Copied
In this case you could create an action script based on export layers as files (sorry french UI)ā
ā
And if you do not specify a prefix (just keep it empty) the file will have the layer name
Copy link to clipboard
Copied
Hello sir, I couldn't understand the language in the images which are you posted, can you please change the Photoshop's language to English and repost the images?
Thankyou sir.
Copy link to clipboard
Copied
No I cannot. Sorry
Here is the way to do that :
Select the layer
Copy link to clipboard
Copied
This is the way I use to save one layer as a jpg/png file.
Right click on the layer
Export As
Copy link to clipboard
Copied
There is a basic problem with your what you want to do. A layer can contain transparency and be larger or smaller then the document canvas size. Jpeg file format does not support transparency only has support for a background layer. That will crop the layer to canvas size fill empty area with background colored pixels and blend in color into transparent areas.
Copy link to clipboard
Copied
// save jpg of active layer;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
try {
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
}
catch (e) {
var theName = myDocument.name;
var thePath = "~/Desktop"
};
var theName = myDocument.activeLayer.name;
// duplicate;
var myDocument = app.activeDocument.duplicate("theCopy", false);
hideOthers ();
// jpg options;
var jpgopts = new JPEGSaveOptions();
jpgopts.embedProfile = true;
jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;
jpgopts.matte = MatteType.NONE;
jpgopts.quality = 10;
myDocument.saveAs((new File(thePath+"/"+theName+".jpg")),jpgopts,true);
// close;
myDocument.close(SaveOptions.DONOTSAVECHANGES);
};
////// hide others //////
function hideOthers () {
// =======================================================
var idShw = charIDToTypeID( "Shw " );
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var list1 = new ActionList();
var ref1 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref1.putEnumerated( idLyr, idOrdn, idTrgt );
list1.putReference( ref1 );
desc2.putList( idnull, list1 );
var idTglO = charIDToTypeID( "TglO" );
desc2.putBoolean( idTglO, true );
executeAction( idShw, desc2, DialogModes.NO );
};
Copy link to clipboard
Copied
Then the is the file name problem
Copy link to clipboard
Copied
Good point!
The layerās name could indeed defie the requirements, so one might have to insert checks to remove the offending parts.
Copy link to clipboard
Copied
There is also the duplicate name problem layer names do not need to be unique many document will have layers with identical layer names like Background. That is why scripts like export layers the files include doc name and layer stack level in new file names.
Copy link to clipboard
Copied
I stole a regular expression from Export Layers for file names for you a layer name invalid characters
~!@#$%^&*()_+`-={}|[]\:";'<>?,./1234567890
will become file name
~!@#$%^&_()_+`-={}_[]__ā;ā___,._1234567890
// save jpg of active layer;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
try {
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
}
catch (e) {
var theName = myDocument.name;
var thePath = "~/Desktop"
};
var theName = myDocument.activeLayer.name.replace(/[:\/\\*\?\"\<\>\|]/g, "_"); // '/\:*?"<>|' -> '_' // from export layers to files
// duplicate;
var myDocument = app.activeDocument.duplicate("theCopy", false);
hideOthers ();
// jpg options;
var jpgopts = new JPEGSaveOptions();
jpgopts.embedProfile = true;
jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;
jpgopts.matte = MatteType.NONE;
jpgopts.quality = 10;
myDocument.saveAs((new File(thePath+"/"+theName+".jpg")),jpgopts,true);
// close;
myDocument.close(SaveOptions.DONOTSAVECHANGES);
};
////// hide others //////
function hideOthers () {
// =======================================================
var idShw = charIDToTypeID( "Shw " );
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var list1 = new ActionList();
var ref1 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref1.putEnumerated( idLyr, idOrdn, idTrgt );
list1.putReference( ref1 );
desc2.putList( idnull, list1 );
var idTglO = charIDToTypeID( "TglO" );
desc2.putBoolean( idTglO, true );
executeAction( idShw, desc2, DialogModes.NO );
};
Copy link to clipboard
Copied
Maybe this answer from Julia would help:
Personnaly I choosed to adress the situation by modifying the "Export Layers as file" Photoshop JSX script in order to get rid of suffixes and then use an action script simply refering to the menu itemā¦ It is a one click solution for the end user. You select the layer and click to launch the action, and that's it.
But all other ways are interesting, specially writing a dedicated JSX script as you did. I will check it, Bravo!
Copy link to clipboard
Copied
First of all thank you to all who have looked at this. So far this one works excellent.
One final question if I may:
Pretend I would like to save the .jpeg file to
C:\MyFolder
How will I do it?
// save jpg of active layer;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
try {
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
}
catch (e) {
var theName = myDocument.name;
var thePath = "~/Desktop"
};
var theName = myDocument.activeLayer.name;
// duplicate;
var myDocument = app.activeDocument.duplicate("theCopy", false);
hideOthers ();
// jpg options;
var jpgopts = new JPEGSaveOptions();
jpgopts.embedProfile = true;
jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;
jpgopts.matte = MatteType.NONE;
jpgopts.quality = 10;
myDocument.saveAs((new File(thePath+"/"+theName+".jpg")),jpgopts,true);
// close;
myDocument.close(SaveOptions.DONOTSAVECHANGES);
};
////// hide others //////
function hideOthers () {
// =======================================================
var idShw = charIDToTypeID( "Shw " );
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var list1 = new ActionList();
var ref1 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref1.putEnumerated( idLyr, idOrdn, idTrgt );
list1.putReference( ref1 );
desc2.putList( idnull, list1 );
var idTglO = charIDToTypeID( "TglO" );
desc2.putBoolean( idTglO, true );
executeAction( idShw, desc2, DialogModes.NO );
};
Copy link to clipboard
Copied
Change the path you could make that selectable with a select folder dialog
var thePath = "~/Desktop"
you should also add the regular expression to the layer name
var theName = myDocument.activeLayer.name.replace(/[:\/\\*\?\"\<\>\|]/g, "_"); // '/\:*?"<>|' -> '_' // from export layers to files
Copy link to clipboard
Copied
// save jpg of active layer;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
try {
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
}
catch (e) {
var theName = myDocument.name;
var thePath = "~/Desktop"
};
var thePath = Folder.selectDialog("Choose a folder", thePath);
var theName = myDocument.activeLayer.name.replace(/[:\/\\*\?\"\<\>\|]/g, "_"); // '/\:*?"<>|' -> '_' // from export layers to files
// duplicate;
var myDocument = app.activeDocument.duplicate("theCopy", false);
hideOthers ();
// jpg options;
var jpgopts = new JPEGSaveOptions();
jpgopts.embedProfile = true;
jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;
jpgopts.matte = MatteType.NONE;
jpgopts.quality = 10;
myDocument.saveAs((new File(thePath+"/"+theName+".jpg")),jpgopts,true);
// close;
myDocument.close(SaveOptions.DONOTSAVECHANGES);
};
////// hide others //////
function hideOthers () {
// =======================================================
var idShw = charIDToTypeID( "Shw " );
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var list1 = new ActionList();
var ref1 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref1.putEnumerated( idLyr, idOrdn, idTrgt );
list1.putReference( ref1 );
desc2.putList( idnull, list1 );
var idTglO = charIDToTypeID( "TglO" );
desc2.putBoolean( idTglO, true );
executeAction( idShw, desc2, DialogModes.NO );
};
Copy link to clipboard
Copied
Thank you, the save-as dialog option is excellent too, it will come in handy š
But I still need to save file directly to C:\MyFolder with no dialog.
I have changed this:
var thePath = "~/Desktop"
to this:
var thePath = "C/MyFolder"
but it still does not work. what am I doing wrong?
Copy link to clipboard
Copied
try
var thePath = "/c/MyFolder"
Copy link to clipboard
Copied
unfortunately no, it is still saving image into original folder that the document was loaded from
Copy link to clipboard
Copied
Update -
this works:
1. create new document in photoshop
2. create new layer
3. draw/scribble anything on layer
when the script runs, it successfully saves the file to C:\MyFolder
BUT- this doesn't work:
1. load an existing .psd file from anywhere on your hard drive
2. create a new layer
3. draw/scribble anyhting on layer
Now when script runs, it does not save to C:\MyFolder, but instead it saves it to original .psd location on hard drive.
How do we do so that it *always* saves to C:\MyFolder no matter if it is new photoshop document or existing one loaded from hard drive?
Copy link to clipboard
Copied
It the top code the us set thepath
you need to override that like I did with the select code
try {
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
}
or remove it
This only run if the document is new has pot path
catch (e) {
var theName = myDocument.name;
var thePath = "~/Desktop"
};
Copy link to clipboard
Copied
Success!
Thanks you to both JJMack and c.pfaffenbichler, you both contribute to the forums are most helpful and invaluable, each and every time.
Here is final code which will save current layer to jpeg in specified folder:
// save jpg of active layer;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theName = myDocument.name;
var thePath = "/c/MyFolder"
var theName = myDocument.activeLayer.name;
// duplicate;
var myDocument = app.activeDocument.duplicate("theCopy", false);
hideOthers ();
// jpg options;
var jpgopts = new JPEGSaveOptions();
jpgopts.embedProfile = true;
jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;
jpgopts.matte = MatteType.NONE;
jpgopts.quality = 10;
myDocument.saveAs((new File(thePath+"/"+theName+".jpg")),jpgopts,true);
// close;
myDocument.close(SaveOptions.DONOTSAVECHANGES);
};
////// hide others //////
function hideOthers () {
// =======================================================
var idShw = charIDToTypeID( "Shw " );
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var list1 = new ActionList();
var ref1 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref1.putEnumerated( idLyr, idOrdn, idTrgt );
list1.putReference( ref1 );
desc2.putList( idnull, list1 );
var idTglO = charIDToTypeID( "TglO" );
desc2.putBoolean( idTglO, true );
executeAction( idShw, desc2, DialogModes.NO );
};
Copy link to clipboard
Copied
Copy link to clipboard
Copied
thanks r-bin, so far is working perfectly. I have not yet come across any problems but I will reply here if trouble occurs with the script. š
Regards,