I have created a script in Photoshop to adjust the scale size of multiple layers, but I am encountering some inaccuracies. The script is designed to resize both the width and height of the selected layers to 76.39%. However, when testing the script, I found that the width changed to 76.92% and the height changed to 75.76% instead.
What could be causing this discrepancy in the script? It is crucial to maintain the aspect ratio while scaling the layers.
// Script for resizing multiple layers
function resizeSelectedLayers() {
var doc = app.activeDocument;
var selectedLayers = [];
// Prompt user to select layers
var selectedLayerIndices = prompt("Enter the indices of the layers you want to resize (comma-separated):", "");
if (selectedLayerIndices === null || selectedLayerIndices === "") {
alert("No layers selected.");
return;
}
// Convert comma-separated indices to array
var indicesArray = selectedLayerIndices.split(",");
for (var i = 0; i < indicesArray.length; i++) {
var index = parseInt(indicesArray[i]);
if (!isNaN(index) && index >= 0 && index < doc.layers.length) {
selectedLayers.push(doc.layers[index]);
} else {
alert("Invalid layer index: " + indicesArray[i]);
return;
}
}
// Check if there are selected layers
if (selectedLayers.length > 0) {
// Resize each selected layer
for (var j = 0; j < selectedLayers.length; j++) {
var selectedLayer = selectedLayers[j];
doc.activeLayer = selectedLayer;
// Calculate scaling factor
var scalePercentage = 76.39;
var scaleFactor = scalePercentage / 100;
// Get current dimensions
var width = selectedLayer.bounds[2] - selectedLayer.bounds[0];
var height = selectedLayer.bounds[3] - selectedLayer.bounds[1];
// Calculate new dimensions with locked aspect ratio
var newWidth = width * scaleFactor;
var newHeight = height * scaleFactor;
// Calculate the scaling ratio
var ratio = newWidth / width;
// Set up transformation parameters
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc.putReference(charIDToTypeID('null'), ref);
// Set transformation values
desc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));
desc.putUnitDouble(charIDToTypeID('Wdth'), charIDToTypeID('#Prc'), 100 * ratio);
desc.putUnitDouble(charIDToTypeID('Hght'), charIDToTypeID('#Prc'), 100 * ratio);
desc.putEnumerated(charIDToTypeID('Intr'), charIDToTypeID('Intp'), charIDToTypeID('Bcbc'));
// Execute transformation
executeAction(charIDToTypeID('Trnf'), desc, DialogModes.NO);
}
} else {
alert("Please select one or more layers.");
}
}
// Ensure there is an active document
if (app.documents.length > 0) {
// Call the resizeSelectedLayers function
resizeSelectedLayers();
} else {
alert("No documents are open.");
}