@Stephen Marsh
The code is excellent and does what is required well and excellent
- But I'd like something else if it doesn't bother you
After the linking process, I want to align the image layer and the shape layer from the top and left because most of the image layers are identical to the shape layers
- If there is a problem in this mode, there is no problem
Thanks for your hard work and effort
Try this 1.1 version:
/*
Apply Clipping Mask to All Smart Object Layers.jsx
v1.1, Stephen Marsh, 14th September 2022
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-that-converts-images-into-smartobjects-and-links-images-to-layer-shapes/td-p/13191501
With special thanks to Christoph Pfaffenbichler for the recursive SO processing framework.
Note: Assumes that there is a pair of layers, with the upper layer a smart object and the lower layer content acting as the source for the clipping mask.
No special checks or validation are performed to ensure that the layers are in pairs.
*/
#target photoshop
var myDocument = app.activeDocument;
myDocument.suspendHistory("processAllSmartObjects", "processSOLayers(myDocument)");
function processSOLayers(theParent) {
for (var m = theParent.layers.length - 1; m >= 0; m--) {
var theLayer = theParent.layers[m];
// apply the function to layers;
if (theLayer.typename == "ArtLayer") {
if (theLayer.kind == LayerKind.SMARTOBJECT) {
var theVisibility = theLayer.visible;
myDocument.activeLayer = theLayer;
app.runMenuItem(stringIDToTypeID('groupEvent'));
selectForwardORBackwardLayer("backwardEnum");
selectionFromVectorMask();
align2Selection('AdLf');
align2Selection('AdTp');
activeDocument.selection.deselect();
theLayer.visible = theVisibility;
}
// Run on the contents of layerSets
} else {
processSOLayers(theLayer)
}
}
return;
}
// Functions
function selectionFromVectorMask() {
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();
reference.putProperty( s2t( "channel" ), s2t( "selection" ));
descriptor.putReference( s2t( "null" ), reference );
reference2.putEnumerated( s2t( "path" ), s2t( "path" ), s2t( "vectorMask" ));
reference2.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
descriptor.putReference( s2t( "to" ), reference2 );
descriptor.putInteger( s2t( "version" ), 1 );
descriptor.putBoolean( s2t( "vectorMaskParams" ), true );
executeAction( s2t( "set" ), descriptor, DialogModes.NO );
}
function selectForwardORBackwardLayer(forwardORbackward) {
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var list = new ActionList();
var reference = new ActionReference();
// "forwardEnum" or "backwardEnum"
reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( forwardORbackward ));
descriptor.putReference( s2t( "null" ), reference );
descriptor.putEnumerated( s2t( "selectionModifier" ), s2t( "selectionModifierType" ), s2t( "addToSelection" ));
descriptor.putBoolean( s2t( "makeVisible" ), false );
list.putInteger( 10 );
list.putInteger( 30 );
descriptor.putList( s2t( "layerID" ), list );
executeAction( s2t( "select" ), descriptor, DialogModes.NO );
}
function align2Selection(method) {
/* https://gist.github.com/MarshySwamp/df372e342ac87854ffe08e79cbdbcbb5 */
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
desc.putReference(charIDToTypeID("null"), ref);
desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
try {
executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
} catch (e) {}
}