@Mohamed Hameed21513110 – as I said, the points are only minor, however, I believe that they are worth discussing as a learning exercise.
1) The script explicitly captures the current ruler units to a variable startRulerUnits, before setting the rulers to pixels. All good so far... However, you didn't set the original ruler units back again at the end of the script. It's a simple and easy mistake to make, I have been there myself. My assumption is that if you didn't want to restore the original rulers in the first place, there would be no need to capture their value, you would have just set the script to change the units to pixels.
Therefore:
app.preferences.rulerUnits = startRulerUnits;
Should be added at the end of the script, before the SaveCurrentConfig function.
2) The script obviously works fine as is, the following just looks like a bit of cleanup/housekeeping:
var cInfo
cInfo = layerWidth + ',' + layerHeight;
I'm not trying to "nit pick" on this point, it just didn't look right, it obviously works as is and I could be missing something? I would clean this to:
var cInfo = layerWidth + ',' + layerHeight;
I haven't looked over the second script in detail.
I am very happy that you could read between the lines and produce the two scripts from my original outline of what I believed was required. The scripts that I made at the time followed the same basic steps, however, I went about things differently to achieve the same end.
Script 2 of 2:
/*
Resize Selected Layers from Source - Script 2 of 2.jsx
Stephen Marsh, v1.0 - 6th January 2022
https://community.adobe.com/t5/photoshop-ecosystem-discussions/copy-and-past-layer-boundary/td-p/12636059
NOTES:
Run this script on multiple target layers to resize using the source layer size from the desktop preference file
*/
//#target photoshop
function main() {
var prefFileIn = File('~/Desktop/_Resize_Layer_Preference_File.txt');
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));
//alert(prefFileWidthValue.toSource());
// Read the 3rd line from the log file & convert the string to a number/integer
var prefFileHeightValue = ~~prefFileIn.readln(3);
//alert(prefFileHeightValue.toSource());
prefFileIn.close();
// Debugging
$.writeln(logInfo);
$.writeln(prefFileWidthValue);
$.writeln(prefFileHeightValue);
processSelectedLayers();
// End of script notification
var layerWidth = (app.activeDocument.activeLayer.bounds[2] - app.activeDocument.activeLayer.bounds[0]);
var layerHeight = (app.activeDocument.activeLayer.bounds[3] - app.activeDocument.activeLayer.bounds[1]);
app.refresh();
alert("Script completed!" + "\r" + "Source Layer Size:" + "\r" + "W: " + prefFileWidthValue + "px" + " H: " + prefFileHeightValue + "px" + "\r" + "Target Layer Size:" + "\r" + "W: " + layerWidth + " H: " + layerHeight);
// 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() {
// Store the original ruler units and set to px
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Layer origin
// ~~ = convert string to integer
var layerX = ~~app.activeDocument.activeLayer.bounds[0].value;
var layerY = ~~app.activeDocument.activeLayer.bounds[1].value;
// 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
app.activeDocument.resizeImage(prefFileWidthValue, prefFileHeightValue, app.activeDocument.resolution, ResampleMethod.BICUBIC);
// Close saving changes
app.activeDocument.close(SaveOptions.SAVECHANGES);
// Align Active Layer to Select All.jsx
align2SelectAll('AdLf');
align2SelectAll('AdTp');
// Reposition the resized layer back to the upper left origin
app.activeDocument.activeLayer.translate(layerX, layerY);
// Convert the smart object back to a layer (less compatible? 'placedLayerConvertToLayers')
app.runMenuItem(stringIDToTypeID('rasterizePlaced'));
// Restore the original ruler units
app.preferences.rulerUnits = savedRuler;
}
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();
}
} else {
app.beep();
alert('There is no valid file named "_Resize_Layer_Preference_File.txt" on the desktop!');
}
}
app.activeDocument.suspendHistory("Resize selected layers script", "main()");