Skip to main content
Stephen Marsh
Community Expert
Community Expert
April 24, 2019
Answered

Adding a Prompt to a Variable

  • April 24, 2019
  • 1 reply
  • 3884 views

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:

 

user-notification-dialogs

 

Hoping somebody can help set me straight, thanks!

This topic has been closed for replies.
Correct answer Laubender

This is your code that I'm using OK with PhotoShop CS6:

(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);

    if( newWidth == null ){ return }; // Test if CANCEL returns null, then do nothing.

    if( newWidth == "" ){ return }; // Test if an empty string is returned, then do nothing.

    // Now hope for the best that prompt returns something that can be easily coerced to a reasonable number

    var newSize = (100 / width) * newWidth;

    activeDocument.activeLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);

    app.preferences.rulerUnits = startRulerUnits;  

})();

My concern is that someone, me?, is typing something to the prompt that cannot be coerced to a number.

Prompt always returns strings when the OK button is used. The default of the prompt is highlighted. If I use backspace and Return an empty string will be returned that also is coerced to 0 and the result is the same: An empty layer. So I included yet another line to test for this.

Regards,
Uwe

1 reply

Community Expert
April 24, 2019

Hi Stephen,

instead of break use return

Also make sure that the return value of the prompt is converted to a number if it is not null.

And if that cannot be done that the function will do nothing. Do that with an error alert and then do return.

Also test the returned value with isNaN( returnValueFromPrompt )

isNaN means is not a number.

But do that test after you tested the return value with null.

Note: isNaN( null ) returns falls !

if( isNaN( newWidth ) ){ return };

FWIW: If I run your code with the added line using break the ESTK will throw an error.

I cannot see that the contents of the active layer is scaled to nothing.

Regards,
Uwe

Community Expert
April 24, 2019

See for isNaN( null ) here:

javascript - Why is isNaN(null) == false in JS? - Stack Overflow

Some food for thought:

isNaN( parseInt(null) ); // returns true

isNaN( Number(null) ); // returns false

isNaN( NaN ); // true

NaN == NaN ; // false

Regards,
Uwe