Skip to main content
Participant
April 26, 2020
Question

Replace Smart Object’s Content And Save PNG

  • April 26, 2020
  • 1 reply
  • 632 views

Hello, I have a script replace smart object’s content and save jpg, But my job was changed. I need change save from JPG to PNG. But I can't do it. Please help me!

Script save to JPG: 

#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
var theLayer = myDocument.activeLayer;
// JPG Options;
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 8;
// Check if layer is SmartObject;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
alert("selected layer is not a smart object")
} else {
// Select Files;
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg;*.png", true)
} else {
var theFiles = File.openDialog("please select files", getFiles, true)
};
if (theFiles) {
var dskTop = Folder.desktop;

var dskPth = String(dskTop);

var newSpot = new File(dskPth+"/Folder Save");

var selectedFolder = newSpot.saveDlg('Select Destination Folder');

var illFilePath = selectedFolder.path;
for (var m = 0; m < theFiles.length; m++) {
// Replace SmartObject
theLayer = replaceContents(theFiles[m], theLayer);
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
// Save JPG
myDocument.saveAs((new File(selectedFolder.path + "/" + theNewName + ".png")), jpgSaveOptions, true,Extension.LOWERCASE);
}
}
}
};
// Get PSDs, TIFs and JPGs from files
function getFiles(theFile) {
if (theFile.name.match(/\.(psd|tif|jpg|png)$/i) != null || theFile.constructor.name == "Folder") {
return true
};
};
// Replace SmartObject Contents
function replaceContents(newFile, theSO) {
app.activeDocument.activeLayer = theSO;
// =======================================================
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc3.putPath(idnull, new File(newFile));
var idPgNm = charIDToTypeID("PgNm");
desc3.putInteger(idPgNm, 1);
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
return app.activeDocument.activeLayer
};
This topic has been closed for replies.

1 reply

JJMack
Community Expert
Community Expert
April 26, 2020

Change the jpeg save option to png save option and change the save as jpeg to save as png and do not forget to  change the save  file extension from jpg to png. photoshop cc javascript reference 

JJMack