Skip to main content
Inspiring
September 20, 2022
Answered

Exclude entering non positive integers and Exit scripts under specific conditions

  • September 20, 2022
  • 1 reply
  • 799 views

Hi,

I have the following script. I want to exclude all input non positive integers. When the input is not a positive integer, stop the script from executing the following code. Can someone help me? Thank you in advance!

 

  function highlightLayerInContainer (targetLayer) {
		var someTempPath = targetLayer.pathItems.rectangle(0, 0, 200, 200);
    someTempPath.name = "TEMP [for highlighting]";
		app.activeDocument.selection =  null;
		someTempPath.selected = true;
		app.doScript("target layer", "Test Set 2"); 
		someTempPath.remove();
  }

var dupN=prompt("Please enter the number of layers to copy:","8","Tips");

if (dupN!=null && dupN!="")  // I want to exclude all non positive integers entered, but I don't know how to write these conditions??
{
   

    for (var i=1;i<dupN;i++) {                                               
        highlightLayerInContainer(app.activeDocument.layers[0].layers[0]); 
        app.doScript("DupeLayer", "Test Set 2");
        app.activeDocument.layers[0].layers[0].name=i+1  
    }
}
else
{
    alert("Please do not press the \"Cancel\" button!!\n\nPlease rerun the script and enter the number of layers to copy!!");  

    // If the input is not a positive integer, I want to stop the script here and stop executing the following code. If I use 'Return', I will be prompted with an error ??
    
}


var doc = app.activeDocument;
var layers = doc.layers;


for (var i = doc.layers.length - 1; i > -1; i--) {

  recursiveRename(doc.layers[i]);
}

function recursiveRename(parent) {
  for (var i = 0; i < parent.layers.length; i++) {
    parent.layers[i].name = parent.layers[i].name.replace(/_复制.*$/, "");
    recursiveRename(parent.layers[i]);
  }
}

 

This topic has been closed for replies.
Correct answer Manan Joshi

Hi @Manan Joshi 

Thank you for your detailed explanation! It is worked well.

 

Do you have a idea for "Exit scripts under specific conditions" ?

 

else
{
    alert("Please do not press the \"Cancel\" button!!\n\nPlease rerun the script and enter the number of layers to copy!!");  

    // If the input is not a positive integer, I want to stop the script here and stop executing the following code. If I use 'Return', I will be prompted with an error ??
    
}

  


Illustrator does not have an exit function as far as I can recall, so you probably will have to do one of the following

  • You can use return, return a value based on which you can decide whether the code at the function call has to further executed or not and keep repeating this in the entire function call chain
  • Throw a new Error and use a try catch block at the outermost level in the code. Something like below

 

 

else
{
    throw new Error("Please do not press the \"Cancel\" button!!\n\nPlease rerun the script and enter the number of layers to copy!!");  
}

In the catch block you can write something like the following
catch(e){
  alert(e) //This should show an alert of the message you sent in the Error
}

 

 

-Manan

1 reply

Community Expert
September 20, 2022

The following code should display a prompt only for +ive numbers

var dupN=prompt("Please enter the number of layers to copy:","8","Tips");
if(dupN && !isNaN(dupN) && dupN > 0)
	alert("+ive number")

-Manan

Inspiring
September 20, 2022

Thanks @Manan Joshi 

 

What is the meaning of the first condition parameter below (only variables are written separately)? What role does it play? I'm a new JS player. Thank you for your reply.

 

 if(dupN && !isNaN(dupN) && dupN > 0)

 

In addition, it cannot exclude decimals, such as 3.65, etc.....

 

 

Community Expert
September 20, 2022

If the user cancels the prompt then dupN would have the value null. null evaluates to false in a conditional statement so if(dupN) would be false if the value is undefined. This would infact also return false for 0 as well.

In fact this isn't needed we could use the following

var dupN=prompt("Please enter the number of layers to copy:","8","Tips");
if(!isNaN(dupN) && dupN > 0 && !dupN.match(/\./))
	alert("+ive whole number")

Here I have added the last condition that searches for presence of . in the string and if we find it then we have a decimal no and we ignore it. The other two conditions before it ensure that it is a number > 0

-Manan