I will likely modify/update the following two scripts, however for now I hope that they serve.
EDIT – 7th August 2022, Scripts updated to 1.3 version:
Step 1 of 2:
/*
Resize Layer from Source - Script 1 of 2.jsx
Stephen Marsh, v1.3 - 7th August 2022
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-do-i-automatically-copy-the-dimensions-and-position-of-one-layer-to-another/td-p/13117557
NOTES:
Run this script on the source layer to copy the layer bounds info to the desktop preference file
*/
#target photoshop
// Store the original ruler units and set to px
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Set the layer dimension variables
var layerWidth = (activeDocument.activeLayer.bounds[2] - activeDocument.activeLayer.bounds[0]);
var layerHeight = (activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1]);
var layerLeft = activeDocument.activeLayer.bounds[0];
var layerRight = activeDocument.activeLayer.bounds[2];
var layerTop = activeDocument.activeLayer.bounds[1];
var layerBottom = activeDocument.activeLayer.bounds[3];
// Log text file platform specific LF options
var os = $.os.toLowerCase().indexOf("mac") >= 0 ? "mac" : "windows";
if (os === "mac") {
prefFileOutLF = "Unix"; // Legacy = "Macintosh"
} else {
prefFileOutLF = "Windows";
}
// Create the preference file
var prefFileOut = new File('~/Desktop/_Resize_Layer_Preference_File.txt');
var dateTime = new Date().toLocaleString();
if (prefFileOut.exists)
prefFileOut.remove();
prefFileOut.open("w");
prefFileOut.encoding = "UTF-8";
prefFileOut.lineFeed = prefFileOutLF;
prefFileOut.writeln(dateTime + ", " + "Source Doc: " + activeDocument.name + ", " + "Source Layer: " + activeDocument.activeLayer.name);
prefFileOut.writeln(layerWidth.value);
prefFileOut.writeln(layerHeight.value);
prefFileOut.writeln(layerLeft.value);
prefFileOut.writeln(layerRight.value);
prefFileOut.writeln(layerTop.value);
prefFileOut.writeln(layerBottom.value);
prefFileOut.close();
// Restore the original ruler units
app.preferences.rulerUnits = savedRuler;
// End of script
alert("Source layer coordinates copied!");
Step 2 of 2:
/*
Resize Layer from Source - Script 2 of 2.jsx
Stephen Marsh, v1.3 - 7th August 2022
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-do-i-automatically-copy-the-dimensions-and-position-of-one-layer-to-another/td-p/13117557
NOTES:
Run this script on multiple target layers to resize using the source layer size from the desktop preference file
*/
#target photoshop
function main() {
// Extra padding value in px: 0 or 100 etc.
var pxPadding = 0;
var prefFileIn = File('~/Desktop/_Resize_Layer_Preference_File.txt');
// Store the original ruler units and set to px
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
if (File(prefFileIn).exists && File(prefFileIn).length > 0) {
try {
// Read the preference file from the user's desktop
prefFileIn.open('r');
// Read the 1st line from the log file, a means to an end...
var logInfo = prefFileIn.readln(1);
// Read the remaining values and convert the string to a number/integer
var prefFileWidthValue = Math.floor(prefFileIn.readln(2));
var prefFileHeightValue = Math.floor(prefFileIn.readln(3));
var prefFileLeftValue = Math.floor(prefFileIn.readln(4));
var prefFileRightValue = Math.floor(prefFileIn.readln(5));
var prefFileTopValue = Math.floor(prefFileIn.readln(6));
var prefFileBottomValue = Math.floor(prefFileIn.readln(7));
prefFileIn.close();
} catch (error) {
alert("An unexpected error occurred when reading the preference file!")
}
processSelectedLayers();
// End of script
app.refresh();
prefFileIn.remove();
app.beep();
//alert("Script completed!");
// Restore the original ruler units
app.preferences.rulerUnits = savedRuler;
// Functions
function processSelectedLayers() {
var s2t = stringIDToTypeID;
(r = new ActionReference).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var lrs = executeActionGet(r).getList(p),
sel = new ActionReference();
for (var i = 0; i < lrs.count; i++) {
sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
(r = new ActionReference).putIdentifier(s2t('layer'), p);
(d = new ActionDescriptor()).putReference(s2t("target"), r);
executeAction(s2t('select'), d, DialogModes.NO);
// Call the resize layer function
resizeLayerFromPrefFile();
}
}
function resizeLayerFromPrefFile() {
// Convert the selected layer to an embedded Smart Object
// A hack to work in px rather than converting to % for use with .resize()
executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);
// Edit the smart object
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
// Resize the smart object using px
activeDocument.resizeImage(prefFileWidthValue + pxPadding, prefFileHeightValue + pxPadding, activeDocument.resolution, ResampleMethod.BICUBIC);
// Close saving changes
activeDocument.close(SaveOptions.SAVECHANGES);
// Convert the smart object back to a layer (doesn't work in older versions)
app.runMenuItem(stringIDToTypeID('placedLayerConvertToLayers'));
// Remove the layer group
removeGroup(false);
// Align
align2SelectAll('AdLf');
align2SelectAll('AdTp');
// Reposition the resized layer
app.activeDocument.activeLayer.translate(prefFileLeftValue, prefFileTopValue);
// Adjust the position after padding
if (pxPadding > 0) {
var positionValue = pxPadding / 2;
activeDocument.activeLayer.translate(-positionValue, -positionValue);
}
}
function align2SelectAll(method) {
/*
AdLf = Align Left
AdRg = Align Right
AdCH = Align Centre Horizontal
AdTp = Align Top
AdBt = Align Bottom
AdCV = Align Centre Vertical
*/
app.activeDocument.selection.selectAll();
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) {}
app.activeDocument.selection.deselect();
}
function removeGroup(deleteContained) {
try {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
descriptor.putBoolean(s2t("deleteContained"), deleteContained);
executeAction(s2t("delete"), descriptor, DialogModes.NO);
} catch (e) {}
}
} else {
app.beep();
alert('There is no valid file named "_Resize_Layer_Preference_File.txt" on the desktop!');
}
}
activeDocument.suspendHistory("Resize selected layers script", "main()");