Importing Remove.bg files as smart object layer including the layermask
A while back @Stephen Marsh created a script for me to import Remove.bg files as layers including a layermask into a template file. At that time, and for that particular case, making the image a smart object was not desirable. It would interfere with the warping process. See the original post here (including examples)
But now I want to re-use the script for a slightly different flow and wish to have the imported layer be a smart object of the original image and, on top of it, the layer mask.
Is somebody or perhaps the master@Stephen Marsh himself 😅 Able to assist me?
/*
Stack Masked Doc Layers to Template Doc.jsx
v1.0 - 21st May 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-import-warp-resize-and-move-layers/td-p/14623443
*/
#target photoshop
function main() {
(function () {
try {
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Set the active doc as the template doc
var templateDoc = app.activeDocument;
var templateDocName = app.activeDocument.name;
// Ensure that the contents of the PHOTOS group is active
if (app.activeDocument.activeLayer.name != 'PHOTOS') {
app.activeDocument.activeLayer = app.activeDocument.layerSets.getByName('PHOTOS').layers[0];
}
// Select the 'remove.bg' PSD source files
var selectFile = File.openDialog('Select the file/s:', Multiselect = true);
if (selectFile === null) {
return;
}
// Loop over the selected files
for (var i = 0; i < selectFile.length; i++) {
// Open each selected file
app.open(File(selectFile[i]));
// Set the sourceDoc as the active document
var sourceDoc = app.activeDocument;
// Select the source layer
//app.activeDocument.activeLayer = app.activeDocument.layers.getByName('remove.bg');
app.activeDocument.activeLayer = app.activeDocument.layers[0];
// Ensure that the RGB channels are selected and not the layer mask
app.activeDocument.activeChannels = app.activeDocument.componentChannels;
// Apply the Background to the remove.bg layer
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "RGB" ));
reference.putName( s2t( "layer" ), "Background" );
descriptor2.putReference( s2t( "to" ), reference );
descriptor.putObject( s2t( "with" ), s2t( "calculation" ), descriptor2 );
executeAction( s2t( "applyImageEvent" ), descriptor, DialogModes.NO );
// Duplicate the source layer to the template doc
dupeLayer(templateDocName, app.activeDocument.name.replace(/\.[^\.]+$/, ''));
// Set the template doc as the active document
app.activeDocument = templateDoc;
// Conditionally resize the layer to fit the canvas
var docWidth = app.activeDocument.width;
var docHeight = app.activeDocument.height;
var layerWidth = (app.activeDocument.activeLayer.bounds[2].value - app.activeDocument.activeLayer.bounds[0].value);
var layerHeight = (app.activeDocument.activeLayer.bounds[3].value - app.activeDocument.activeLayer.bounds[1].value);
if (layerWidth > docWidth) {
//alert('The layer is wider than the canvas!');
app.activeDocument.activeLayer.resize((docWidth / layerWidth) * 100, (docWidth / layerWidth) * 100, AnchorPosition.MIDDLECENTER);
}
if (layerHeight > docHeight) {
//alert('The layer is taller than the canvas!');
app.activeDocument.activeLayer.resize((docHeight / layerHeight) * 100, (docHeight / layerHeight) * 100, AnchorPosition.MIDDLECENTER);
}
// Centre the duped layer on the template canvas
app.activeDocument.activeLayer.translate(docWidth / 2 - (app.activeDocument.activeLayer.bounds[0] + app.activeDocument.activeLayer.bounds[2]) / 2,
docHeight / 2 - (app.activeDocument.activeLayer.bounds[1] + app.activeDocument.activeLayer.bounds[3]) / 2);
// Close the source doc
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
}
app.preferences.rulerUnits = savedRuler;
// Functions
function dupeLayer(targetDocName, sourceLayerName) {
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var list = new ActionList();
var reference = new ActionReference();
var reference2 = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
reference2.putName(s2t("document"), targetDocName);
descriptor.putReference(s2t("to"), reference2);
descriptor.putString(s2t("name"), sourceLayerName);
descriptor.putList(s2t("ID"), list);
executeAction(s2t("duplicate"), descriptor, DialogModes.NO);
}
} catch (e) {
alert('Error!' + '\r' + e + ', Line: ' + e.line);
}
})();
}
main();
