I have a script on Photoshop that allows me to resize a layer to the image bounds while maintaining the proportions.
The code is the seguent:
var Document = app.activeDocument;
var BND = app.activeDocument.activeLayer.bounds;
var IHeight = (BND[3] - BND[1]);
var IWidth = (BND[2] - BND[0]);
if (IWidth >= IHeight) {
Document.activeLayer.resize ((Document.width / IWidth * 100), (Document.width / IWidth * 100), AnchorPosition.MIDDLECENTER);
}
else {
Document.activeLayer.resize ((Document.height / IHeight * 100), (Document.height / IHeight * 100), AnchorPosition.MIDDLECENTER);
}
The problem is that it only resizes the active layer.
I'm trying to integrate the seguent piece of code that selects all the layers:
var NAD = new ActionDescriptor ();
var NAR = new ActionReference ();
NAR.putEnumerated (charIDToTypeID ("Lyr "), charIDToTypeID ("Ordn"), charIDToTypeID ("Trgt"));
NAD.putReference (charIDToTypeID ("null"), NAR);
executeAction (stringIDToTypeID ("selectAllLayers"), NAD, DialogModes.NO);
What I want to do is resize all the layers together so that they can keep the same aspect ratio between each other, so i can't resize with a loop or the proportions would be lost by transforming all layers to the same size as the canvas.
Do you have any ideas how this could be done?
I have already tried to replace activeLayer with layers or artLayers, but I get "Error 1302: No such element" and I don't know how to address the problem.
Since I wasn't able to resolve, I ultimately opted to group the layers and ungroup them once the resizing was done. The final code is the seguent:
var Document = app.activeDocument;
// Select all layers with Action Manager
(NAR = new ActionReference ()).putEnumerated (ID ("layer"), ID ("ordinal"), ID ("targetEnum"));
(NAD = new ActionDescriptor ()).putReference (ID ("null"), NAR);
executeAction (ID ("selectAllLayers"), NAD, DialogModes.NO);
// Group selected layers with Action Manager
(NAR = new ActionReference ()).putClass (ID ("layerSection"));
(NAD = new ActionDescriptor ()).putReference (ID ("null"), NAR);
(NAR2 = new ActionReference ()).putEnumerated (ID ("layer"), ID ("ordinal"), ID ("targetEnum"));
NAD.putReference (ID ("from"), NAR2);
(NAR3 = new ActionDescriptor ()).putString (ID ("name"), """Temporary_Group""");
NAD.putObject (ID ("using"), ID ( "layerSection" ), NAR3);
executeAction (ID ("make"), NAD, DialogModes.NO);
}
// Select temporary group with Action Manager
(NAR = new ActionReference ()).putName (ID ("layer"), "Temporary_Group");
(NAD = new ActionDescriptor ()).putReference (ID ("null"), NAR);
executeAction (ID ("select"), NAD, DialogModes.NO);
var BND = app.activeDocument.activeLayer.bounds;
var IHeight = (BND[3] - BND[1]);
var IWidth = (BND[2] - BND[0]);
if (IWidth >= IHeight) {
Document.activeLayer.resize ((Document.width / IWidth * 100), (Document.width / IWidth * 100), AnchorPosition.MIDDLECENTER);
}
else {
Document.activeLayer.resize ((Document.height / IHeight * 100), (Document.height / IHeight * 100), AnchorPosition.MIDDLECENTER);
}
// Delete temporary group with Action Manager
(NAR = new ActionReference ()).putEnumerated (ID ("layer"), ID ("ordinal"), ID ("targetEnum"));
(NAD = new ActionDescriptor ()).putReference (ID ("null"), NAR);
executeAction (ID ("ungroupLayersEvent"), NAD, DialogModes.NO);
}
// Deselect all layers with Action Manager
(NAR = new ActionReference ()).putEnumerated (ID ("layer"), ID ("ordinal"), ID ("targetEnum"));
(NAD = new ActionDescriptor ()).putReference (ID ("null"), NAR);
executeAction (ID ("selectNoLayers"), NAD, DialogModes.NO);