Exclude entering non positive integers and Exit scripts under specific conditions

Contributor ,
Sep 19, 2022 Sep 19, 2022

Copy link to clipboard

Copied

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]);
  }
}

 

TOPICS
Scripting

Views

81

Likes

Translate

Translate

Report

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 2 Correct answers

Community Expert , Sep 20, 2022 Sep 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

...

Likes

Translate

Translate
Community Expert , Sep 20, 2022 Sep 20, 2022

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
...

Likes

Translate

Translate
Community Expert ,
Sep 19, 2022 Sep 19, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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
Contributor ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

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.....

 

 

Likes

Translate

Translate

Report

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 ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

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

 

Likes

Translate

Translate

Report

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
Contributor ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

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 ??
    
}

  

Likes

Translate

Translate

Report

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 ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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
Contributor ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

Thanks @Manan Joshi 

I do this, but It prompt me of an error:

 

RaymondZJH_0-1663731598934.png

 

Likes

Translate

Translate

Report

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 ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

The error says it, catch block should have a corresonding try block as well. Read about try/catch online it is very easy to use. If you still have issues then post your whole code snippet here I will fix it for you.

-Manan

Likes

Translate

Translate

Report

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
Contributor ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

LATEST

Thanks you very much! @Manan Joshi  I got it now👍👍

Likes

Translate

Translate

Report

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