Adding a Prompt to a Variable
I am a beginner in scripting, self taught – finding it easier to learn from examples when I stumble over one that interests me.
I found the following script:
// https://graphicdesign.stackexchange.com/questions/58812/specific-resize-layer-action
#target photoshop
(function (){
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var bounds = activeDocument.activeLayer.bounds;
var width = bounds[2].value - bounds[0].value;
var newSize = (100 / width) * 300; // 300px
activeDocument.activeLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);
app.preferences.rulerUnits = startRulerUnits;
})();
To which I added a prompt (line 08) to enter a user defined variable rather than a hard coded value.
// https://graphicdesign.stackexchange.com/questions/58812/specific-resize-layer-action
#target photoshop
(function (){
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var bounds = activeDocument.activeLayer.bounds;
var width = bounds[2].value - bounds[0].value;
var newWidth = prompt("Enter a new width in pixels.", 300);
var newSize = (100 / width) * newWidth;
activeDocument.activeLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);
app.preferences.rulerUnits = startRulerUnits;
})();
All is good if one hits OK, but if one presses Cancel – the actual active layer that should be resized has all content deleted. My guess is that cancel returns a value of null and that null is not a number so therefore the layer content is scaled to nothing.
So I tried adding the following line of code suggested in an Illustrator scripting discussion, however it does not stop the layer content being removed:
// User pressed the Cancel button:
if( newWidth == null ){ break };
I read up on prompts, however I didn’t find anything that helped:
Hoping somebody can help set me straight, thanks!

