I’m trying to make a script that will take an element on a layer and restore its size to 100%.
This is what I have so far but it doesn’t seem to be working on smart objects for some reason. Am I missing something?
try {
var doc = app.activeDocument;
var layers = doc.artLayers;
var size = dialog();
for (var i = 0; i < doc.artLayers.length -1; i++) {
var activeLayer = doc.artLayers.getByName(doc.artLayers[i].name);
var orUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PERCENT;
activeLayer.resize(size, size, AnchorPosition.MIDDLECENTER);
app.preferences.rulerUnits = orUnits;
}
} catch (e) {
alert( e );
}
function dialog() {
// Dialog box
var myWindow = new Window("dialog", "Resize Each Layer");
// Keeps things inline
myWindow.orientation = "row";
// Informational text
myWindow.add("statictext", undefined, "New size ( percentage ):");
// This is the box where the size is inserted
var myText = myWindow.add("edittext", undefined, "");
myText.characters = 5;
myText.active = true;
// Ok
myWindow.add("button", undefined, "OK");
if (myWindow.show() == 1) return myText.text;
}
It doesn’t work whenever I try and run it and it does nothing to smart objects at all.
4
Answers
A couple things first…
I tweaked your script minimally to get it to do something, however, depending on what you want it may still be inadequate. You say you want to “reset scale of Smart Object to 100%.” The approach you are taking does not do that. What you (almost) completed was a script that changes the layers on the screen to a percentage of their CURRENT size. Assuming this is what you intend, all is good.
Next, one reason your code wasn’t working is because you reference
layer
on line 8. You never (from the code you posted) created a variable calledlayer
meaning the compiler has no idea what to do with that code. Actualy by simply deleting the two if statements in the beginning of your code, it will run.Here is “working” code:
Also, this code currently does not check for layer type, so problems will arise. Your next best step to account for this would probably be to add layer-checking INSIDE your for loop so that your script checks the layer within your
layers
array.I also added the
doc.artLayers.length -1
to your for loop so that it would not try to resize the background layer. You should remove that “-1
” once you add layer-checking. Let me know if I can further clarify anything for you.layer.resize() is a relative resize, not absolute, so if you tell it to resize by 100%, that won’t do anything–you’re telling it to resize to its current size. You’d need to know the current scale to do it that way (which I don’t think is exposed).
I don’t think there’s any way to reset a smart object’s scale with a script or recorded action. Smart objects aren’t very well supported by Photoshop’s APIs…
The only known to me way of resetting smart object scale is to replace current smart layer with the original temporary psb file (reference file). To do that you first need to find smart object
fileReference
(name of a temporary psb file, it’s going to be in temporary folder, you’ll find it in layer action descriptor), then place it in your document (using File > Place Embedded), put it to coordinates of the original layer and then remove the original layer. Here’s an algorithm:By adapting the code I found here I was able to get the unscaled dimensions of the active layer using an ActionReference; https://community.adobe.com/t5/photoshop/how-can-i-capture-a-smart-object-s-original-dimension-via-scripts/td-p/9679328?page=1
You can then use these dimensions with the layer bounds to resize() up to 100%