There is an issue with Free Transform not matching the document dimensions.
- June 5, 2026
- 3 replies
- 63 views
There's an issue with this Photoshop script. When opening a Photoshop.psd file containing layers labeled A2 and A3, the current document dimensions are 2484 pixels wide by 3512 pixels high. The script is designed to scale the active layer to match the document size exactly. While the A3 smart object layer scales correctly with this script, the A2 layer ends up with dimensions of 2482 pixels wide by 3510 pixels high after running—slightly off from the document size. I don't want the layer to be rasterized; although using Free Transform after rasterization produces correct dimensions, I need to keep it as a smart object layer. Running the script twice in succession does yield the correct result, but it's inefficient. I'd appreciate any alternative solutions—thank you all for your help.
var doc = app.activeDocument;
var length = parseFloat(doc.width); // Get document width
var width = parseFloat(doc.height); // Get document height
var layer = doc.activeLayer;
// Get and adjust layer size (based on document width and height)
var layerBounds = layer.bounds;
var layerWidth = layerBounds[2].value - layerBounds[0].value;
var layerHeight = layerBounds[3].value - layerBounds[1].value;
var newWidth = (length / layerWidth) * 100; // Adjust layer width based on document width
var newHeight = (width / layerHeight) * 100; // Adjust layer height based on document height
layer.resize(newWidth, newHeight); // Free transform to adjust layer size
// Re-acquire the scaled layer bounds
layerBounds = layer.bounds;
// Calculate layer center point coordinates
var layerCenterX = (layerBounds[2].value + layerBounds[0].value) / 2;
var layerCenterY = (layerBounds[3].value + layerBounds[1].value) / 2;
// Calculate document center point coordinates
var documentCenterX = doc.width.value / 2;
var documentCenterY = doc.height.value / 2;
// Calculate horizontal and vertical distance to move
var deltaX = documentCenterX - layerCenterX;
var deltaY = documentCenterY - layerCenterY;
// Move layer to document center point
layer.translate(deltaX, deltaY);
// Scale image to match document dimensions and center it in the document
