-2

I'm trying to speed up my workflow. What i have is a picture of food, that needs to be exported with different colored backgrounds.

Right now I'm hiding the colored background layers one by one, as i export the jpgs. But i feel there has to be a quicker way to do this?

Any help or tips would be much appreciated.

T3RR0R
  • 2,747
  • 3
  • 10
  • 25
  • Assuming that all the layers that you need are in the PSD, and it's just a matter of switching off certain backgrounds then yes. – Ghoul Fool Nov 23 '21 at 20:08

1 Answers1

1

Assuming the bottom most layer is the Background Layer, above that you have three images which are coloured backgrounds. Above those is your art. it's just a case of identifying which layers are which and first switching them all OFF and then ON on turn.

var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;
var n = (numOfLayers - backgrounds.length)-1;

var backgrounds = ["Red", "Yellow", "Blue"];

// switch backgrounds OFF

for (var i = n; i < numOfLayers-1; i++)
{
  srcDoc.layers[i].visible = false;
}

// switch them ON one at at time
for (var i = n; i < numOfLayers-1; i++)
{
  srcDoc.layers[i].visible = true;

  // save
  var myFileName = "C:\\temp\\my_picture_" + i + ".jpg";
  save_as_jpg(myFileName);

  // Switch it off again
  srcDoc.layers[i].visible = false;
}


function save_as_jpg(afilepath)
{
   duplicate_it();

   // Flatten the jpg
   activeDocument.flatten();

   // jpg file options
   var jpgFile = new File(afilepath);
   jpgSaveOptions = new JPEGSaveOptions();
   jpgSaveOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
   jpgSaveOptions.embedColorProfile = true;
   jpgSaveOptions.matte = MatteType.NONE;
   jpgSaveOptions.quality = 12;

   activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);

   //close without saving
   app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}


function duplicate_it()
{
  // duplicate image into new document
  var str = "temp";

  var id428 = charIDToTypeID( "Dplc" );
  var desc92 = new ActionDescriptor();
  var id429 = charIDToTypeID( "null" );
  var ref27 = new ActionReference();
  var id430 = charIDToTypeID( "Dcmn" );
  var id431 = charIDToTypeID( "Ordn" );
  var id432 = charIDToTypeID( "Frst" );
  ref27.putEnumerated( id430, id431, id432 );
  desc92.putReference( id429, ref27 );
  var id433 = charIDToTypeID( "Nm  " );
  desc92.putString( id433, str ); // name
  executeAction( id428, desc92, DialogModes.NO );
}
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125