Skip to main content
Participant
October 16, 2018
Question

i want to change image save jpeg to png How can i do it in this Script ? Please Help

  • October 16, 2018
  • 3 replies
  • 883 views

// select folder of mock-ups, if they contain exactly one smart object replace its content with selected designs and save jpg-files;

// 2017, use it at your own risk;

#target photoshop

var theFolder = Folder.selectDialog ("select folder");

if (theFolder) {

var theFiles = theFolder.getFiles(/\.(psb|tif|psd)$/i);

if (theFiles.length > 0) {

//var theDesigns = theFolder.getFiles(/\.(psb|jpg|tif|psd)$/i);

// select files;

if ($.os.search(/windows/i) != -1) {var theDesigns = File.openDialog ("please select files", "*.psd;*.psb;*.tif", true)}

else {var theDesigns = File.openDialog ("please select files", getFiles, true)};

if (theDesigns.length > 0) {

for (var x = 0; x < theFiles.length; x++) {

replaceSmartObject(theFiles, theDesigns);

}

}

}

};

////// replace smart object content if only one smart object exists //////

function replaceSmartObject (theFile, theDesigns) {

var myDocument = app.open(theFile);

var theSmartObjects = collectSmartObjects();

// if only one smart object;

if (theSmartObjects.length == 1) {

var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];

var thePath = myDocument.path;

var theLayer = theSmartObjects[0][0];

myDocument.activeLayer = theLayer;

// psd options;

psdOpts = new PhotoshopSaveOptions();

psdOpts.embedColorProfile = true;

psdOpts.alphaChannels = true;

psdOpts.layers = true;

psdOpts.spotColors = true;

// jpg options;

var jpegOptions = new JPEGSaveOptions();

jpegOptions.quality = 2;

jpegOptions.embedColorProfile = true;

jpegOptions.matte = MatteType.NONE;

// work through the array;

for (var m = 0; m < theDesigns.length; m++) {

// replace smart object;

theLayer = replaceContents (theDesigns, theLayer);

var theNewName = theDesigns.name.match(/(.*)\.[^\.]+$/)[1];

//save jpg;

myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".png")),jpegOptions,true);

}

};

myDocument.close(SaveOptions.DONOTSAVECHANGES)

};

////// get psds, tifs and jpgs from files //////

function getFiles (theFile) {

if (theFile.name.match(/\.(psd|tif|psb)$/i) != null || theFile.constructor.name == "Folder") {

return true

};

};

////// replace 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

};

////// select smart objects, based on paul riggott’s code //////

function collectSmartObjects(){

var theSmartObjects = new Array;

if(!documents.length) {return};

selectAllLayers();

var selectedLayers = getSelectedLayersIdx();

for(var a = 0; a < selectedLayers.length; a++){

if(hasSmartObject(Number(selectedLayers))){

makeActiveByIndex(Number(selectedLayers),false);

theSmartObjects.push([app.activeDocument.activeLayer, a])

}

};

return theSmartObjects;

function hasSmartObject(idx){

var ref = new ActionReference();

ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "smartObject" ));

ref.putIndex( charIDToTypeID( "Lyr " ), idx);

var desc = executeActionGet(ref);

if(desc.hasKey(stringIDToTypeID('smartObject'))) {return true}

else {return false};

};

function makeActiveByIndex( idx, visible ){

    var desc = new ActionDescriptor();

      var ref = new ActionReference();

      ref.putIndex(charIDToTypeID( "Lyr " ), idx);

      desc.putReference( charIDToTypeID( "null" ), ref );

      desc.putBoolean( charIDToTypeID( "MkVs" ), visible );

   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );

};

function getSelectedLayersIdx(){

  var selectedLayers = new Array;

  var ref = new ActionReference();

  ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

  var desc = executeActionGet(ref);

  if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){

  desc = desc.getList( stringIDToTypeID( 'targetLayers' ));

  var c = desc.count;

  var selectedLayers = new Array();

  for(var i=0;i<c;i++){

  try{

  activeDocument.backgroundLayer;

  selectedLayers.push(  desc.getReference( i ).getIndex() );

  }catch(e){

  selectedLayers.push(  desc.getReference( i ).getIndex()+1 );

  }

  }

  }else{

  var ref = new ActionReference();

  ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));

  ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

  try{

  activeDocument.backgroundLayer;

  selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);

  }catch(e){

  selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));

  };

  };

  return selectedLayers;

};

function selectAllLayers() {

var desc29 = new ActionDescriptor();

  var ref23 = new ActionReference();

  ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

desc29.putReference( charIDToTypeID('null'), ref23 );

executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );

};

};

This topic has been closed for replies.

3 replies

Inspiring
October 17, 2018

// select folder of mock-ups, if they contain exactly one smart object replace its content with selected designs and save jpg-files;

// 2017, use it at your own risk;

#target photoshop

var theFolder = Folder.selectDialog ("select folder");

if (theFolder) {

var theFiles = theFolder.getFiles(/\.(psb|tif|psd)$/i);

if (theFiles.length > 0) {

//var theDesigns = theFolder.getFiles(/\.(psb|jpg|tif|psd)$/i);

// select files;

if ($.os.search(/windows/i) != -1) {var theDesigns = File.openDialog ("please select files", "*.psd;*.psb;*.tif", true)}

else {var theDesigns = File.openDialog ("please select files", getFiles, true)};

if (theDesigns.length > 0) {

for (var x = 0; x < theFiles.length; x++) {

replaceSmartObject(theFiles, theDesigns);

}

}

}

};

////// replace smart object content if only one smart object exists //////

function replaceSmartObject (theFile, theDesigns) {

var myDocument = app.open(theFile);

var theSmartObjects = collectSmartObjects();

// if only one smart object;

if (theSmartObjects.length == 1) {

var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];

var thePath = myDocument.path;

var theLayer = theSmartObjects[0][0];

myDocument.activeLayer = theLayer;

// psd options;

psdOpts = new PhotoshopSaveOptions();

psdOpts.embedColorProfile = true;

psdOpts.alphaChannels = true;

psdOpts.layers = true;

psdOpts.spotColors = true;

// png options;

var pngOptions = new PNGSaveOptions();

with (pngOptions) {

    embedColorProfile = true; // change to false if required

    compression = 8 // add compression value

    interlaced = false // change to true if required

    };

// work through the array;

for (var m = 0; m < theDesigns.length; m++) {

// replace smart object;

theLayer = replaceContents (theDesigns, theLayer);

var theNewName = theDesigns.name.match(/(.*)\.[^\.]+$/)[1];

//save png;

myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".png")),pngOptions,true);

}

};

myDocument.close(SaveOptions.DONOTSAVECHANGES)

};

////// get psds, tifs and jpgs from files //////

function getFiles (theFile) {

if (theFile.name.match(/\.(psd|tif|psb)$/i) != null || theFile.constructor.name == "Folder") {

return true

};

};

////// replace 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

};

////// select smart objects, based on paul riggott’s code //////

function collectSmartObjects(){

var theSmartObjects = new Array;

if(!documents.length) {return};

selectAllLayers();

var selectedLayers = getSelectedLayersIdx();

for(var a = 0; a < selectedLayers.length; a++){

if(hasSmartObject(Number(selectedLayers))){

makeActiveByIndex(Number(selectedLayers),false);

theSmartObjects.push([app.activeDocument.activeLayer, a])

}

};

return theSmartObjects;

function hasSmartObject(idx){

var ref = new ActionReference();

ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "smartObject" ));

ref.putIndex( charIDToTypeID( "Lyr " ), idx);

var desc = executeActionGet(ref);

if(desc.hasKey(stringIDToTypeID('smartObject'))) {return true}

else {return false};

};

function makeActiveByIndex( idx, visible ){

    var desc = new ActionDescriptor();

      var ref = new ActionReference();

      ref.putIndex(charIDToTypeID( "Lyr " ), idx);

      desc.putReference( charIDToTypeID( "null" ), ref );

      desc.putBoolean( charIDToTypeID( "MkVs" ), visible );

   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );

};

function getSelectedLayersIdx(){

  var selectedLayers = new Array;

  var ref = new ActionReference();

  ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

  var desc = executeActionGet(ref);

  if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){

  desc = desc.getList( stringIDToTypeID( 'targetLayers' ));

  var c = desc.count;

  var selectedLayers = new Array();

  for(var i=0;i<c;i++){

  try{

  activeDocument.backgroundLayer;

  selectedLayers.push(  desc.getReference( i ).getIndex() );

  }catch(e){

  selectedLayers.push(  desc.getReference( i ).getIndex()+1 );

  }

  }

  }else{

  var ref = new ActionReference();

  ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));

  ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

  try{

  activeDocument.backgroundLayer;

  selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);

  }catch(e){

  selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));

  };

  };

  return selectedLayers;

};

function selectAllLayers() {

var desc29 = new ActionDescriptor();

  var ref23 = new ActionReference();

  ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

desc29.putReference( charIDToTypeID('null'), ref23 );

executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );

};

};

c.pfaffenbichler
Community Expert
Community Expert
October 17, 2018

This is an example for exporting a png-24 via Script:

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

// getting the name and location;

var docName = myDocument.name;

var basename = docName.match(/(.*)\.[^\.]+$/)[1];

//getting the location;

var docPath = myDocument.path;

// save png;

savePNG (myDocument, docPath, basename, "_a");

};

////// function to png //////

function savePNG (myDocument, docPath, basename, theSuffix) {

// weboptions;

var webOptions = new ExportOptionsSaveForWeb();

webOptions.format = SaveDocumentType.PNG;

webOptions.PNG8 = false;

webOptions.transparency = true;

webOptions.interlaced = 0;

webOptions.includeProfile = false;

webOptions.optimized = true;

myDocument.exportDocument(new File(docPath+"/"+basename+theSuffix+".png"), ExportType.SAVEFORWEB, webOptions);

};

Participant
October 17, 2018

i added but its not working can you please add this code to script please

c.pfaffenbichler
Community Expert
Community Expert
October 17, 2018
i added but its not working

»Added« – what do you mean?

Unless you show the code one cannot tell what you did exactly.

pixxxelschubser
Community Expert
Community Expert
October 16, 2018

[ moved from Forum comments  to Photoshop Scripting  ]

It seems this thread based on a script written by c.pfaffenbichler​ in Re: RePlace My Design In All Mockups SmartObjects

and the TO has changed some parts of the original script - but not correct

Participant
October 16, 2018

yes but how can i change to save images to png

Stephen Marsh
Community Expert
Community Expert
October 17, 2018

Would this be a “Save As” or “Export” or “Save for Web” PNG?