Skip to main content
Participant
July 19, 2024
Answered

script to return background layer name as variable, then use portion of name for rest of script

  • July 19, 2024
  • 2 replies
  • 318 views

Hi everyone! thank you in advance for any help on this - I'm totally stuck! 

 

I converted an complex action to a script - This script creates 4 image files from a template file named "filename-zTemplate.psd" - this works great, but we have 25 different background images. These background files are labeled "background-1a, background-1b, background-2a, background-2b, etc." and are inserted into each template file as smart objects and moved to be the bottom layer. 

 

I would like to use the 2-digit code in the background layer name for a couple of things: 1) include in the file names of the 4 output images, and 2) to "hide" the background layer of one of the images that is exported as PNG. 

 

My thinking is to pull the background layer name into a variable, then get another variable for the 2-digit code - but I'm probably overthinking after many hours or looking for ways of doing this... Any help is appreciated and I truly thank you in advance! 

 

Here is my code along with comments. 

 

#target photoshop

cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };

// identify the open document
var mainDoc = app.activeDocument;

// identify the name, removing -zTemplate.psd so other files are named properly
var baseName = mainDoc.name.substr(0,mainDoc.name.length-14);

// identify the folder, which is where everything will be saved to
var basePath = activeDocument.path;

// background files are named "background-#z.jpg" - they are inserted as smart objects. We only need the last 2 digits for the next part...

// create variable for background layer name and then get last 2 digits for use where noted. I used "bcode" throughout the script to show how I want to use. E.g. var bcode = background layer's last 2 digits


function Bio_Photo_Create_2024() {
  // Hide
  function step1(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var list1 = new ActionList();
    var ref1 = new ActionReference();
    ref1.putName(cTID('Lyr '), "Guide");
    list1.putReference(ref1);
    desc1.putList(cTID('null'), list1);
    executeAction(cTID('Hd  '), desc1, dialogMode);
  };

  // Save
  function step2(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    executeAction(cTID('save'), undefined, dialogMode);
  };

  // Canvas Size
  function step3(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1550);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Bttm'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step4(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1500);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Top '));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step5(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'), 1800);
    desc1.putEnumerated(cTID('Hrzn'), cTID('HrzL'), cTID('Cntr'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Save
  function step6(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putInteger(cTID('EQlt'), 12);
    desc2.putBoolean(cTID('Optm'), true);
    desc2.putEnumerated(cTID('MttC'), cTID('MttC'), cTID('None'));
    desc1.putObject(cTID('As  '), sTID("JPEGFormat"), desc2);
    desc1.putPath(cTID('In  '), new File (basePath + "/" + baseName + "-hires-" + bcode + ".jpg")); // file name is now basename-hires-3b.jpg assuming background layer is "background-3b"
    desc1.putInteger(cTID('DocI'), 2153);
    desc1.putBoolean(cTID('Cpy '), true);
    desc1.putBoolean(cTID('LwCs'), true);
    executeAction(cTID('save'), desc1, dialogMode);
  };

  // Select
  function step7(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putOffset(cTID('HstS'), -3);
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, dialogMode);
  };

  // Canvas Size
  function step8(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1550);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Top '));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Image Size
  function step9(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1000);
    desc1.putUnitDouble(cTID('Rslt'), cTID('#Rsl'), 72);
    desc1.putBoolean(sTID("scaleStyles"), true);
    desc1.putBoolean(cTID('CnsP'), true);
    desc1.putEnumerated(cTID('Intr'), cTID('Intp'), sTID("automaticInterpolation"));
    executeAction(sTID('imageSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step10(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'), 1000);
    desc1.putEnumerated(cTID('Hrzn'), cTID('HrzL'), cTID('Cntr'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Save
  function step11(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putInteger(cTID('EQlt'), 9);
    desc2.putBoolean(cTID('Optm'), true);
    desc2.putEnumerated(cTID('MttC'), cTID('MttC'), cTID('None'));
    desc1.putObject(cTID('As  '), sTID("JPEGFormat"), desc2);
    desc1.putPath(cTID('In  '), new File (basePath + "/" + baseName + "-quals-" + bcode + ".jpg"));
    desc1.putInteger(cTID('DocI'), 2153);
    desc1.putBoolean(cTID('Cpy '), true);
    desc1.putBoolean(cTID('LwCs'), true);
    executeAction(cTID('save'), desc1, dialogMode);
  };

  // Select
  function step12(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putOffset(cTID('HstS'), -3);
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, dialogMode);
  };

  // Canvas Size
  function step13(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1550);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Top '));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step14(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1500);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Bttm'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step15(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1400);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Top '));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Image Size
  function step16(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 434);
    desc1.putUnitDouble(cTID('Rslt'), cTID('#Rsl'), 72);
    desc1.putBoolean(sTID("scaleStyles"), true);
    desc1.putBoolean(cTID('CnsP'), true);
    desc1.putEnumerated(cTID('Intr'), cTID('Intp'), sTID("automaticInterpolation"));
    executeAction(sTID('imageSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step17(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'), 616);
    desc1.putEnumerated(cTID('Hrzn'), cTID('HrzL'), cTID('Cntr'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Save
  function step18(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putInteger(cTID('EQlt'), 10);
    desc2.putBoolean(cTID('Optm'), true);
    desc2.putEnumerated(cTID('MttC'), cTID('MttC'), cTID('None'));
    desc1.putObject(cTID('As  '), sTID("JPEGFormat"), desc2);
    desc1.putPath(cTID('In  '), new File (basePath + "/" + baseName + "-web-" + bcode + ".jpg"));
    desc1.putInteger(cTID('DocI'), 2153);
    desc1.putBoolean(cTID('Cpy '), true);
    desc1.putBoolean(cTID('LwCs'), true);
    executeAction(cTID('save'), desc1, dialogMode);
  };

  // Select
  function step19(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putOffset(cTID('HstS'), -5);
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, dialogMode);
  };

  // Canvas Size
  function step20(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1510);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Bttm'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Image Size
  function step21(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'), 990);
    desc1.putUnitDouble(cTID('Rslt'), cTID('#Rsl'), 72);
    desc1.putBoolean(sTID("scaleStyles"), true);
    desc1.putBoolean(cTID('CnsP'), true);
    desc1.putEnumerated(cTID('Intr'), cTID('Intp'), sTID("automaticInterpolation"));
    executeAction(sTID('imageSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step22(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 698);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Top '));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Hide
  function step23(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var list1 = new ActionList();
    var ref1 = new ActionReference();
    ref1.putName(cTID('Lyr '), ("Background-" + bcode));
    list1.putReference(ref1);
    desc1.putList(cTID('null'), list1);
    executeAction(cTID('Hd  '), desc1, dialogMode);
  };

  // Save
  function step24(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putEnumerated(cTID('Mthd'), sTID("PNGMethod"), sTID("quick"));
    desc2.putEnumerated(sTID("PNGInterlaceType"), sTID("PNGInterlaceType"), sTID("PNGInterlaceNone"));
    desc2.putEnumerated(sTID("PNGFilter"), sTID("PNGFilter"), sTID("PNGFilterAdaptive"));
    desc2.putInteger(cTID('Cmpr'), 6);
    desc2.putEnumerated(sTID("embedIccProfileLastState"), sTID("embedOff"), sTID("embedOff"));
    desc1.putObject(cTID('As  '), sTID("PNGFormat"), desc2);
    desc1.putPath(cTID('In  '), new File (basePath + "/" + baseName + "-ind-" + bcode + ".png"));
    desc1.putInteger(cTID('DocI'), 2463);
    desc1.putBoolean(cTID('Cpy '), true);
    desc1.putBoolean(cTID('LwCs'), true);
    desc1.putBoolean(cTID('EmbP'), true);
    executeAction(cTID('save'), desc1, dialogMode);
  };

  // Select
  function step25(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putOffset(cTID('HstS'), -3);
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, dialogMode);
  };

  // Save
  function step26(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    executeAction(cTID('save'), undefined, dialogMode);
  };


  step1();      // Hide Guide Layer
  step2();      // Save
  step3();      // Canvas Size to 1550px h from bottom center
  step4();      // Canvas Size to 1500px h from top center
  step5();      // Canvas Size to 1800px w from center
  step6();      // Save as JPG 12 quality
  step7();      // Select history between steps 2 and 3
  step8();      // Canvas Size to 1550px h from top center
  step9();      // Image Size to 1000px h constrained proportions
  step10();      // Canvas Size to 1000px w from center
  step11();      // Save as JPG 9 quality
  step12();      // Select history between steps 2 and 3
  step13();      // Canvas Size to 1550px h from top center
  step14();      // Canvas Size to 1500px h from bottom center
  step15();      // Canvas Size to 1400px h from top center
  step16();      // Image Size to 434px h constrained proportions at 72dpi
  step17();      // Canvas Size to 616px w from center
  step18();      // Save as JPG 10 quality
  step19();      // Select history between steps 2 and 3
  step20();      // Canvas Size to 1510px h from bottom center
  step21();      // Image Size to 990px w constrained proportions at 72dpi
  step22();      // Canvas Size to 698px h from top center
  step23();      // Hide layer Blurred-Back
  step24();      // Save as PNG with transparent background
  step25();      // Select history between steps 2 and 3
  step26();      // Save
};


// Execute the script

Bio_Photo_Create_2024();

 

This topic has been closed for replies.
Correct answer Michael38703612673i

I did a little more sleuthing and used your solution combined with a couple others I found to achieve my end result. Notably the following:

 

// identify all of the layers
var layerBack = app.activeDocument.artLayers

// Return the full name of the background layer with identifying 2-digit code
for (var i = 0; i < layerBack.length; i++) {
  if ((RegExp(".*TextToFind.*")).test(layerBack[i].name)) {
    var bgndName = layerBack[i].name;
  }
}

// Pull just the background identifier 2-digit code
var bcode = bgndName.substr(bgndName.length - 2, 2);

 

So with this solve, I have bgndName, which is the full name of the layer containing my partial text, and I have bcode, which is just the 2-digit identifier. Everything saves with the 2-digit code AND I'm able to hide the necessary layer for the PNG step. I can put this script into an action and it just does everything for me! yay!

 

Thank you for the help! I think this means I have what I need! 🙂 

 

#target photoshop

cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };

// identify the open document
var mainDoc = app.activeDocument;

// identify the name, removing -zTemplate.psd so other files are named properly
var baseName = mainDoc.name.substr(0,mainDoc.name.length-14);

// identify the folder, which is where everything will be saved to
var basePath = activeDocument.path;

// identify all of the layers
var layerBack = app.activeDocument.artLayers

// Return the full name of the background layer with identifying 2-digit code
for (var i = 0; i < layerBack.length; i++) {
  if ((RegExp(".*TextToFind.*")).test(layerBack[i].name)) {
    var bgndName = layerBack[i].name;
  }
}

// Pull just the background identifier 2-digit code
var bcode = bgndName.substr(bgndName.length - 2, 2);


function Bio_Photo_Create_2024() {
  // Hide
  function step1(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var list1 = new ActionList();
    var ref1 = new ActionReference();
    ref1.putName(cTID('Lyr '), "Guide");
    list1.putReference(ref1);
    desc1.putList(cTID('null'), list1);
    executeAction(cTID('Hd  '), desc1, dialogMode);
  };

  // Save
  function step2(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    executeAction(cTID('save'), undefined, dialogMode);
  };

  // Canvas Size
  function step3(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1550);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Bttm'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step4(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1500);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Top '));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step5(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'), 1800);
    desc1.putEnumerated(cTID('Hrzn'), cTID('HrzL'), cTID('Cntr'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Save
  function step6(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putInteger(cTID('EQlt'), 12);
    desc2.putBoolean(cTID('Optm'), true);
    desc2.putEnumerated(cTID('MttC'), cTID('MttC'), cTID('None'));
    desc1.putObject(cTID('As  '), sTID("JPEGFormat"), desc2);
    desc1.putPath(cTID('In  '), new File (basePath + "/" + baseName + "-hires-" + bcode + ".jpg"));
    desc1.putInteger(cTID('DocI'), 2153);
    desc1.putBoolean(cTID('Cpy '), true);
    desc1.putBoolean(cTID('LwCs'), true);
    executeAction(cTID('save'), desc1, dialogMode);
  };

  // Select
  function step7(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putOffset(cTID('HstS'), -3);
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, dialogMode);
  };

  // Canvas Size
  function step8(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1550);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Top '));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Image Size
  function step9(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1000);
    desc1.putUnitDouble(cTID('Rslt'), cTID('#Rsl'), 72);
    desc1.putBoolean(sTID("scaleStyles"), true);
    desc1.putBoolean(cTID('CnsP'), true);
    desc1.putEnumerated(cTID('Intr'), cTID('Intp'), sTID("automaticInterpolation"));
    executeAction(sTID('imageSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step10(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'), 1000);
    desc1.putEnumerated(cTID('Hrzn'), cTID('HrzL'), cTID('Cntr'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Save
  function step11(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putInteger(cTID('EQlt'), 9);
    desc2.putBoolean(cTID('Optm'), true);
    desc2.putEnumerated(cTID('MttC'), cTID('MttC'), cTID('None'));
    desc1.putObject(cTID('As  '), sTID("JPEGFormat"), desc2);
    desc1.putPath(cTID('In  '), new File (basePath + "/" + baseName + "-quals-" + bcode + ".jpg"));
    desc1.putInteger(cTID('DocI'), 2153);
    desc1.putBoolean(cTID('Cpy '), true);
    desc1.putBoolean(cTID('LwCs'), true);
    executeAction(cTID('save'), desc1, dialogMode);
  };

  // Select
  function step12(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putOffset(cTID('HstS'), -3);
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, dialogMode);
  };

  // Canvas Size
  function step13(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1550);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Top '));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step14(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1500);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Bttm'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step15(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1400);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Top '));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Image Size
  function step16(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 434);
    desc1.putUnitDouble(cTID('Rslt'), cTID('#Rsl'), 72);
    desc1.putBoolean(sTID("scaleStyles"), true);
    desc1.putBoolean(cTID('CnsP'), true);
    desc1.putEnumerated(cTID('Intr'), cTID('Intp'), sTID("automaticInterpolation"));
    executeAction(sTID('imageSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step17(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'), 616);
    desc1.putEnumerated(cTID('Hrzn'), cTID('HrzL'), cTID('Cntr'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Save
  function step18(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putInteger(cTID('EQlt'), 10);
    desc2.putBoolean(cTID('Optm'), true);
    desc2.putEnumerated(cTID('MttC'), cTID('MttC'), cTID('None'));
    desc1.putObject(cTID('As  '), sTID("JPEGFormat"), desc2);
    desc1.putPath(cTID('In  '), new File (basePath + "/" + baseName + "-web-" + bcode + ".jpg"));
    desc1.putInteger(cTID('DocI'), 2153);
    desc1.putBoolean(cTID('Cpy '), true);
    desc1.putBoolean(cTID('LwCs'), true);
    executeAction(cTID('save'), desc1, dialogMode);
  };

  // Select
  function step19(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putOffset(cTID('HstS'), -5);
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, dialogMode);
  };

  // Canvas Size
  function step20(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 1510);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Bttm'));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Image Size
  function step21(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'), 990);
    desc1.putUnitDouble(cTID('Rslt'), cTID('#Rsl'), 72);
    desc1.putBoolean(sTID("scaleStyles"), true);
    desc1.putBoolean(cTID('CnsP'), true);
    desc1.putEnumerated(cTID('Intr'), cTID('Intp'), sTID("automaticInterpolation"));
    executeAction(sTID('imageSize'), desc1, dialogMode);
  };

  // Canvas Size
  function step22(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 698);
    desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Top '));
    executeAction(sTID('canvasSize'), desc1, dialogMode);
  };

  // Hide
  function step23(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);	  
    var desc1 = new ActionDescriptor();
    var list1 = new ActionList();
    var ref1 = new ActionReference();
    ref1.putName(cTID('Lyr '), bgndName);
    list1.putReference(ref1);
    desc1.putList(cTID('null'), list1);
    executeAction(cTID('Hd  '), desc1, dialogMode);
  };

  // Save
  function step24(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putEnumerated(cTID('Mthd'), sTID("PNGMethod"), sTID("quick"));
    desc2.putEnumerated(sTID("PNGInterlaceType"), sTID("PNGInterlaceType"), sTID("PNGInterlaceNone"));
    desc2.putEnumerated(sTID("PNGFilter"), sTID("PNGFilter"), sTID("PNGFilterAdaptive"));
    desc2.putInteger(cTID('Cmpr'), 6);
    desc2.putEnumerated(sTID("embedIccProfileLastState"), sTID("embedOff"), sTID("embedOff"));
    desc1.putObject(cTID('As  '), sTID("PNGFormat"), desc2);
    desc1.putPath(cTID('In  '), new File (basePath + "/" + baseName + "-ind-" + bcode + ".png"));
    desc1.putInteger(cTID('DocI'), 2463);
    desc1.putBoolean(cTID('Cpy '), true);
    desc1.putBoolean(cTID('LwCs'), true);
    desc1.putBoolean(cTID('EmbP'), true);
    executeAction(cTID('save'), desc1, dialogMode);
  };

  // Select
  function step25(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putOffset(cTID('HstS'), -3);
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, dialogMode);
  };

  // Save
  function step26(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    executeAction(cTID('save'), undefined, dialogMode);
  };


  step1();      // Hide Guide Layer
  step2();      // Save
  step3();      // Canvas Size to 1550px h from bottom center
  step4();      // Canvas Size to 1500px h from top center
  step5();      // Canvas Size to 1800px w from center
  step6();      // Save as JPG 12 quality
  step7();      // Select history between steps 2 and 3
  step8();      // Canvas Size to 1550px h from top center
  step9();      // Image Size to 1000px h constrained proportions
  step10();      // Canvas Size to 1000px w from center
  step11();      // Save as JPG 9 quality
  step12();      // Select history between steps 2 and 3
  step13();      // Canvas Size to 1550px h from top center
  step14();      // Canvas Size to 1500px h from bottom center
  step15();      // Canvas Size to 1400px h from top center
  step16();      // Image Size to 434px h constrained proportions at 72dpi
  step17();      // Canvas Size to 616px w from center
  step18();      // Save as JPG 10 quality
  step19();      // Select history between steps 2 and 3
  step20();      // Canvas Size to 1510px h from bottom center
  step21();      // Image Size to 990px w constrained proportions at 72dpi
  step22();      // Canvas Size to 698px h from top center
  step23();      // Hide layer Blurred-Back
  step24();      // Save as PNG with transparent background
  step25();      // Select history between steps 2 and 3
  step26();      // Save
};


// Execute the script

Bio_Photo_Create_2024();

 

 

2 replies

Legend
July 19, 2024

You already know which background you are putting into each file, so you already have the name. Just use a regex or split() or string operation to grab the last two digits.

 

var foo = 'abcde';
foo = foo.substr(foo.length - 2, 2);

 

Participant
July 19, 2024

Thank you! That is helpful, but I have a question:

 

The second line makes sense... but wouldn't the first line require updating the script with the background name manually each time? (apologies if I'm missing something)

 

I'm thinking of scenarios where we maybe need to batch alongside a script that randomly places the background files into several templates. This means we wouldn't know which is which. 

 

It's more complex code, but is there a way to have "var background = layer with 'background' in the layer name" kind of thing? This way the script can just run and create everything on its own (also easier to batch). 

Legend
July 19, 2024

AT some point you had to name your layers, that's the opportunity to grab the name and store it in a variable. Or yes iterate through layers and find the one that matches.

Participant
July 19, 2024

Also, just to help things since that is a very long code without a ton of context - the steps are listed with comments about what they do, but the steps in question are 6, 11, 18 and 24 for saving files using the code, and step 23 is for hiding the layer. 

 

I prefer to use the layer name rather than a blanket "hide the bottom layer" type thing - unfortunately, we work with contractors and they might move the guide layer to the bottom or something weird like that, so it's ideal to work from layer names - I'm open to suggestions, though, if that's just not possible.