Skip to main content
Known Participant
October 27, 2019
Answered

How to add the condition to this script?

  • October 27, 2019
  • 3 replies
  • 930 views

Hello, please help.

How to add the condition to this script: if the height is from n to n (for example 13000-14000), then ...

 

function getLayerSizeToCopy(){
    var layer = activeDocument.activeLayer; //Grab the currently selected layer

    // Calculate length and width based on the rectangular bounds of the selected layer
    var length = layer.bounds[2]-layer.bounds[0]; //Grab the length
    var width = layer.bounds[3]-layer.bounds[1]; //Grab the width

    // Remove pixels from the length/width "200 px" => "200"
    length = length.toString().replace(' px', '');
    width = width.toString().replace(' px', '');

    prompt( "Layer Size: ", length + ", " + width );
}
getLayerSizeToCopy();
This topic has been closed for replies.
Correct answer Chuck Uebele

I'm not sure what you want to do with the conditional if statement, but here it is in you script, with an alert letting you know if the layer height is between 100 and 1000, and another alert if the response to the prompt is with the range of 100 to 1000:

function getLayerSizeToCopy(){
    var layer = activeDocument.activeLayer; //Grab the currently selected layer

    // Calculate length and width based on the rectangular bounds of the selected layer
    var length = layer.bounds[2]-layer.bounds[0]; //Grab the length
    var width = layer.bounds[3]-layer.bounds[1]; //Grab the width

    // Remove pixels from the length/width "200 px" => "200"
    length = length.toString().replace(' px', '');
    width = width.toString().replace(' px', '');
  
    if(width>100&&width<1000){//tells you if the layer size is within the range of 100 to 1000
        alert('height is in range')
        }
    else{alert('height is NOT in range')}

    var promptResult = prompt( "Layer Size: ", length + ", " + width );
    
    var promptArray = promptResult.replace(/\s/g, "").split(',');

    if(promptArray[1]>100&&promptArray[1]<1000){alert ('The number entered is in range')}//Tells you if the response to the prompt is within the range of 100 to 1000
    else{alert('The number entered is out of range.')}
    
    
}
getLayerSizeToCopy();

3 replies

Chuck Uebele
Community Expert
Community Expert
October 27, 2019

As I mentioned before about your use of width and length, which are considered the same thing. You should use these terms:

Known Participant
October 28, 2019

The code was taken from the network, it was also not convenient for me that the value was different, but at first I couldn’t change it. With your help I changed it to the height and removed some values. Everything works great.

Thank you so much!

Chuck Uebele
Community Expert
Community Expert
October 28, 2019

Glad it's working, and good that you changed the values, as it's confusing the way it was originally written.

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
October 27, 2019

I'm not sure what you want to do with the conditional if statement, but here it is in you script, with an alert letting you know if the layer height is between 100 and 1000, and another alert if the response to the prompt is with the range of 100 to 1000:

function getLayerSizeToCopy(){
    var layer = activeDocument.activeLayer; //Grab the currently selected layer

    // Calculate length and width based on the rectangular bounds of the selected layer
    var length = layer.bounds[2]-layer.bounds[0]; //Grab the length
    var width = layer.bounds[3]-layer.bounds[1]; //Grab the width

    // Remove pixels from the length/width "200 px" => "200"
    length = length.toString().replace(' px', '');
    width = width.toString().replace(' px', '');
  
    if(width>100&&width<1000){//tells you if the layer size is within the range of 100 to 1000
        alert('height is in range')
        }
    else{alert('height is NOT in range')}

    var promptResult = prompt( "Layer Size: ", length + ", " + width );
    
    var promptArray = promptResult.replace(/\s/g, "").split(',');

    if(promptArray[1]>100&&promptArray[1]<1000){alert ('The number entered is in range')}//Tells you if the response to the prompt is within the range of 100 to 1000
    else{alert('The number entered is out of range.')}
    
    
}
getLayerSizeToCopy();
Chuck Uebele
Community Expert
Community Expert
October 27, 2019

Length is normally considered width. I normally use height. Also, you changed the measurement values to a string. You should keep them as numbers, like;

var layerHeight = layer.bounds[3].value-layer.bounds[1].value;

For your question try this (I'm using layerHeight:

if(layerHeight>13000&&layerHeight<14000){// code here

}

Known Participant
October 27, 2019

Thank you, redid the script at your prompt, it worked! 🙂