Copy link to clipboard
Copied
- First : Copy the code / I want a code when choosing a specific layer and standing on it, it copies the dimensions of the layer and saves it
- Second : Paste the code/I want when selecting other layers and executing the code, it will scale the layer to the same dimensions as the layer the values were copied to
This idea is similar to that of Copy layerStyle and Past layerStyle
I'll help you to break down your request into sections. You can then find the code for each section and combine the various bits of code together. This is only how I would do things, those with greater knowledge and experience would do things differently.
This help is offered under the concept of:
“Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime.”
@Mohamed Hameed wrote:...
- First : Copy the code / I want a code when choo
I found the solution that gives me the desired result and this is what I wanted
Thank you all
First : Copy the code
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theBounds = app.activeDocument.activeLayer.bounds;
var layerWidth = theBounds[2] - theBounds[0];
var layerHeight = theBounds[3] - theBounds[1];
var ConfigFile = "~/Desktop/pref.txt";
var cInfo
cInfo = layerWidth +','+layerHeight ;
SaveCurrentConfig(cInfo, ConfigFile);
function
...
Apologies, I have been caught up on other things...
Here is the version that I knocked together, script 1 of 2. I hope that it is helpful, different yet similar. If any point needs an explanation, just ask.
/*
Resize Layer from Source - Script 1 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 action on the source layer to copy the layer bounds info to t
...
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(p
...
Copy link to clipboard
Copied
Stop creating similar threads, like you used to do in the passed year. Just yestarday you made this one:
Paste the copied image inside the selection
Additionally remember this forum is not to write scripts for others but mainly to help to finish already provided, solve unique problems and suggest methods to take.
The only you do is each time to request new scripts, written from zero. Now this becomes ridiciolous. You ask of very simple code to share. One of many you can find yourself on this forum in different forms, or easily learn from Photoshop Scripting Reference.
Make some effort to create code you need, and come back when you get stuck.
Copy link to clipboard
Copied
Sorry, but the title of this article and what I want here is different from what I want in the other article
Copy link to clipboard
Copied
It's variation of that other request that can be continued by alike methods.
The things you're asking are easily searchable on this forum, but you don't do effort to find it. Still you can use mentioned reference to achieve your goal, without aksing constantly 'to tie your shoelaces'. Show your code you used / wrote so far...
Copy link to clipboard
Copied
Lol..I'm really looking for what I want, and if I'm writing what I want, it's because I'm searching, but the results aren't what I want.
- And I saw that I did not like that you mentioned your name among the names of people and you were one of them, so why do you reprimand me and say such things to me?
If you do not want to help, it is enough that you do not insult people in this way, and if you know the solution to the problem, it will benefit you because the correct answer belongs to you and you will not miss a thing. It matters if you attach the solution and know that this solution will be seen by a lot of people like me
Finally, thank you for your interest
Copy link to clipboard
Copied
You don't look for help, but people who will work for you for free, including those who would better search forums for snippets to combine them together, as that's of one possible ways 😉
Copy link to clipboard
Copied
I'll help you to break down your request into sections. You can then find the code for each section and combine the various bits of code together. This is only how I would do things, those with greater knowledge and experience would do things differently.
This help is offered under the concept of:
“Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime.”
@Mohamed Hameed wrote:
- First : Copy the code / I want a code when choosing a specific layer and standing on it, it copies the dimensions of the layer and saves it
1A: Find code which collects the layer bounds of a selected layer to a variable
1B: Find code which saves a variable or other info to a .txt "log" file
This will be one separate script (or a conditional part of a single script that uses a keyboard modifier key press when run, but that is more advanced, I'd just stick with two separate scripts to begin with, baby steps).
@Mohamed Hameed wrote:
- Second : Paste the code/I want when selecting other layers and executing the code, it will scale the layer to the same dimensions as the layer the values were copied to
This idea is similar to that of Copy layerStyle and Past layerStyle
2A: Find code which reads the info from the text log file and saves it to a variable.
2B: Find code which resizes a selected layer's bounds using the info in the variable which was read from the text file
2C: Once you can do this for a single layer, you would then need to find code examples for multiple selected layers, just start with a single layer, again baby steps
Copy link to clipboard
Copied
Thank you for marking my answer as correct.
Before you start your code hunt for the key steps outlined, and before we explore how to adapt the code you find and tie it all together... Do you have any questions, comments etc, on my previous post and or the proposed code hunt?
Copy link to clipboard
Copied
@Mohamed Hameed – how are you going? Just start on Step 1A, don't try to find everything first. One step at a time. :]
Copy link to clipboard
Copied
Thank you for your interest and cooperation
How respectful and helpful you are, and I would like to tell you that before I write any article here, I am looking very carefully for the right way to solve the problem. I only write the problem if I don't find a solution to it or find a solution to it, but I don't know how the code works.
Thanks again
Copy link to clipboard
Copied
Hello Mohamed, I understand and it is a big step from actions to scripting. Just start by searching the forum or the internet for keywords such as "layer bounds" or "Photoshop layer bounds script" etc. You will likely find the code uses anywhere from 2-6 lines. Step 1A is to find the width/height of the source layer. I'm sure that you can meet me part of the way here...
Copy link to clipboard
Copied
I appreciate your cooperation and effort in providing information
But do I have a question?
If I want to write in JavaScript, do I need to learn this language or is it a library within JavaScript, and how do I start learning this language
I would be very grateful if you could provide me information
Copy link to clipboard
Copied
I'll let you know once I learn it too! I don't have a programming background, I just couldn't always do what I wanted to with actions, so scripting was the next step. It would help if I knew JavaScript, but I currently have no need or interest to do so outside of Adobe automation. So I learn by needing to do something. This is a hobby for me. I can write some simple JS from scratch, but this is generally a process of reading Adobe docs for specifics, or general JS docs or tutorials like Mozilla or W3C etc. Not formally knowing JS, I often need to hack away at example code which usually helps me more than manuals do. It is generally a case of copying existing code, modifying it for the current use case.
Copy link to clipboard
Copied
In fact, what I do is search and find some codes and modify them according to what I want..
Thank you very much and I am very grateful for your cooperation and interest
Copy link to clipboard
Copied
I found the solution that gives me the desired result and this is what I wanted
Thank you all
First : Copy the code
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theBounds = app.activeDocument.activeLayer.bounds;
var layerWidth = theBounds[2] - theBounds[0];
var layerHeight = theBounds[3] - theBounds[1];
var ConfigFile = "~/Desktop/pref.txt";
var cInfo
cInfo = layerWidth +','+layerHeight ;
SaveCurrentConfig(cInfo, ConfigFile);
function SaveCurrentConfig(cInfo, ConfigFile){
var F = new File(ConfigFile);
F.open("w");
F.write(cInfo);
F.close();
}
Second : Paste the code
var thePref = "~/Desktop/pref.txt";
if (File (thePref).exists == true) {
var theText = readPref (thePref);
Width = theText.split(',')[0];
Hight = theText.split(',')[1];
//- Resize Layer With input Value - //
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var bounds = activeDocument.activeLayer.bounds;
var width = bounds[2].value - bounds[0].value;
var height = bounds[3].value - bounds[1].value;
var newWidth = (100 / width) * Width;
var newHeight = (100 / height) * Hight;
activeDocument.activeLayer.resize(newWidth, newHeight, AnchorPosition.MIDDLECENTER);
app.preferences.rulerUnits = startRulerUnits;
}
function readPref (thePath) {
if (File(thePath).exists == true) {
var file = File(thePath);
file.open("r", "TEXT", "????");
file.encoding= 'BINARY';
var theText = new String;
for (var m = 0; m < file.length; m ++) {
theText = theText.concat(file.readch());
};
file.close();
return String(theText)
}
};
Copy link to clipboard
Copied
@Mohamed Hameed – Great job!
You took this further than I expected, kudos to you.
I have a couple of very minor critique points on the first script if that is OK? I only mention this as I have made these mistakes in the past and may well do so in the future!
Copy link to clipboard
Copied
Thank you for your encouragement
I accept any criticism as long as it is constructive criticism and aims to learn from my mistakes
And you, you know, I'm not very deep into the codes, but when I wrote this code, I searched a lot and tried the code and discovered the errors and worked on them until I came up with the code in a way that fit what I wanted
So let me know what's wrong so I can fix it
Copy link to clipboard
Copied
@Mohamed Hameed – 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.
Copy link to clipboard
Copied
Apologies, I have been caught up on other things...
Here is the version that I knocked together, script 1 of 2. I hope that it is helpful, different yet similar. If any point needs an explanation, just ask.
/*
Resize Layer from Source - Script 1 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 action 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 = (app.activeDocument.activeLayer.bounds[2] - app.activeDocument.activeLayer.bounds[0]);
var layerHeight = (app.activeDocument.activeLayer.bounds[3] - app.activeDocument.activeLayer.bounds[1]);
// 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: " + app.activeDocument.name + ", " + "Source Layer: " + app.activeDocument.activeLayer.name);
prefFileOut.writeln(layerWidth.value);
prefFileOut.writeln(layerHeight.value);
prefFileOut.close();
// Restore the original ruler units
app.preferences.rulerUnits = savedRuler;
// End of script notification
alert("Source Layer Size:" + "\r" + "W: " + layerWidth + " H: " + layerHeight);
Copy link to clipboard
Copied
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()");
Copy link to clipboard
Copied
First of all, thank you for your interest
But I noticed something that the codes are too long.. I don't know why they are
Second, in the second code: I noticed that the code works on recent versions, while my code works on all versions.
Copy link to clipboard
Copied
Also I noticed that the code I wrote is the problem I had because it only works on one layer.. but when I select a number of layers it doesn't work correctly
Copy link to clipboard
Copied
@Mohamed Hameed wrote:
First of all, thank you for your interest
But I noticed something that the codes are too long.. I don't know why they are
Second, in the second code: I noticed that the code works on recent versions, while my code works on all versions.
The code is as long as it needs to be by design or by my current level of knowledge, either through use of //comments and or formatting for easier reading, or in the case of recorded scripting listener plug-in action manager code this will always be verbose, even when run through the clean sl script. You need to be more specific as to what you consider is "too long".
There can be version compatibility issues, for example the second script leverages smart objects so wouldn't work on a version that didn't support this feature. Again, specifics would need to be known to work out why it works on one version or platform and not another,