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]);
}
}
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
...Illustrator does not have an exit function as far as I can recall, so you probably will have to do one of the following
else
{
throw new Error("Please do not press the \"Cancel\" button!!\n\nPlease
...
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
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.....
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
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 ?? }
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
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thanks you very much! @Manan Joshi I got it now👍👍