Update - 19th December 2022:
/* The previous code using the $.setenv() code has been removed due to errors */
Here is a single OPT/ALT version of the updated two-part script:
/*
Resize Target Layer to Source Layer Size - ALT-OPT.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/is-it-possible-to-copy-all-width-height-x-and-y-properties/td-p/13410970
v1.1, 19th December 2022, Stephen Marsh
*/
#target photoshop
if (documents.length) {
/* Step 2 - Hold down alt/opt to resize the layer */
if (ScriptUI.environment.keyboardState.altKey) {
function main() {
try {
var prefFileIn = File('~/Desktop/_Resize_Layer_Preference_File.txt');
} catch (e) {
alert(e);
}
if (File(prefFileIn).exists && File(prefFileIn).length > 0) {
// 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 2nd line from the log file & convert the string to a number/integer
var prefFileWidthValue = Math.floor(prefFileIn.readln(2));
// Read the 3rd line from the log file & convert the string to a number/integer
var prefFileHeightValue = Math.floor(prefFileIn.readln(3));
var prefFileSourceLayer = prefFileIn.readln(4);
prefFileIn.close();
processSelectedLayers();
prefFileIn.remove();
// End of script notification
//app.refresh();
alert("Script completed!" + "\r" + "Target layer resized to source layer size/position");
// 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();
// Apply any layer styles from source layer to target layer
try {
var idpasteEffects = stringIDToTypeID("pasteEffects");
executeAction(idpasteEffects, undefined, DialogModes.NO);
} catch (error) {
//alert(error);
}
}
}
function resizeLayerFromPrefFile() {
// Store the original ruler units and set to px
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Create variables to hold the target layer dimensions
var targetLayerWidth = activeDocument.activeLayer.bounds[2] - activeDocument.activeLayer.bounds[0];
var targetLayerHeight = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];
// Calculate the % adjustment value
var targetLayerScaleWidth = prefFileWidthValue / targetLayerWidth * 100;
var targetLayerScaleHeight = prefFileHeightValue / targetLayerHeight * 100;
// Transform from upper left
transform(0, 0, targetLayerScaleWidth, targetLayerScaleHeight);
// Alignment
targetLayer = activeDocument.activeLayer;
selectLayerById(prefFileSourceLayer);
transparencySelection();
activeDocument.activeLayer = targetLayer;
align2Selection('AdLf');
align2Selection('AdTp');
activeDocument.selection.deselect();
// Restore the original ruler units
app.preferences.rulerUnits = savedRuler;
// Functions
function transparencySelection() {
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("channel"), s2t("channel"), s2t("transparencyEnum"));
descriptor.putReference(s2t("to"), reference2);
executeAction(s2t("set"), descriptor, DialogModes.NO);
}
function align2Selection(method) {
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) {}
}
function transform(horizontal, vertical, width, height) {
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
descriptor.putEnumerated(s2t("freeTransformCenterState"), s2t("quadCenterState"), s2t("QCSCorner0"));
descriptor2.putUnitDouble(s2t("horizontal"), s2t("pixelsUnit"), horizontal);
descriptor2.putUnitDouble(s2t("vertical"), s2t("pixelsUnit"), vertical);
descriptor.putObject(s2t("offset"), s2t("offset"), descriptor2);
descriptor.putUnitDouble(s2t("width"), s2t("percentUnit"), width);
descriptor.putUnitDouble(s2t("height"), s2t("percentUnit"), height);
descriptor.putEnumerated(s2t("interfaceIconFrameDimmed"), s2t("interpolationType"), s2t("bicubicAutomatic"));
executeAction(s2t("transform"), descriptor, DialogModes.NO);
}
function selectLayerById(id)
/* https://graphicdesign.stackexchange.com/questions/130739/photoshop-scripting-applying-changes-only-to-selected-artboards */
{
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putIdentifier(charIDToTypeID('Lyr '), id);
desc1.putReference(charIDToTypeID('null'), ref1);
executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
}
}
} 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()");
/* Step 1 - Set the target group and layer */
} else {
// Store the original ruler units and set to px
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Set the layer dimension variables
try {
var idcopyEffects = stringIDToTypeID("copyEffects");
executeAction(idcopyEffects, undefined, DialogModes.NO);
var iddisableLayerStyle = stringIDToTypeID("disableLayerStyle");
executeAction(iddisableLayerStyle, undefined, DialogModes.NO);
} catch (error) {
//alert(error);
}
var layerWidth = (activeDocument.activeLayer.bounds[2] - activeDocument.activeLayer.bounds[0]);
var layerHeight = (activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1]);
var sourceLayer = activeDocument.activeLayer.id;
try {
var idpasteEffects = stringIDToTypeID("pasteEffects");
executeAction(idpasteEffects, undefined, DialogModes.NO);
} catch (error) {
//alert(error);
}
// 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(sourceLayer);
prefFileOut.close();
// Restore the original ruler units
app.preferences.rulerUnits = savedRuler;
// End of script notification
alert("Select the target layer/s and run the script again while holding down the ALT/OPT key to transform.");
}
} else {
alert("A document must be open to run this script!");
}