script to return background layer name as variable, then use portion of name for rest of script
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();