Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to add the condition to this script?

Explorer ,
Oct 27, 2019 Oct 27, 2019

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();
TOPICS
Actions and scripting
964
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 27, 2019 Oct 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.bound
...
Translate
Adobe
Community Expert ,
Oct 27, 2019 Oct 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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 27, 2019 Oct 27, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2019 Oct 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();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2019 Oct 27, 2019

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

width and height.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 27, 2019 Oct 27, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2019 Oct 27, 2019
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines