Copy link to clipboard
Copied
I'm using this script to generate a random number. It works fine but I would like to modify it to include a No, Cancel, Yes button. As you can see I figured out placing the "3" into the script gives me the three buttons. But, the random number still gets placed into the m12 field, regardless of what button is pressed. Can someone show me how to make the modification so I'll be able to have the choice as to what I want to do? Here's my existing script.
case "ID Number":
// custom funcitons to generate random integers and random strings;
function RandomInt(nMax, nMin) {
// generate a random integer between the nMin and nMax values
if(typeof nMin == "undefined") nMin = 0;
nMin = parseInt(nMin);
nMax = parseInt(nMax);
return Math.floor(Math.random() * (nMax - nMin)) + nMin;
} // end RandomInt function
function RandomString(cValues, nLength) {
// generate a random string of length nLength from string of cValues;
nlength = parseInt(nLength);
// convert string of values to an an array;
var aValue = cValues.split("");
// array for results of random characters;
var aResult = new Array()
// random intiger;
var nRandom;
// populate array with 6 random values;
for(i = 0; i < nLength; i++) {
// get random number;
nRandom = RandomInt(aValue.length);
// insert random letter into array;
aResult = aValue[nRandom];
} // end of populate array;
// convert array of values to a string;
return aResult.join("");
} //
// end custom funcitons;
// possible values for items in the field
var cString = "ABCDEF0123456789";
// generate a random string of values;
var cResult = RandomString(cString, 7);
// display the random string
// populate a field with the value;
app.alert("Enter unique Proposal Number:\n\n" + cResult, 0, 3); ///this produces the No, Cancel, Yes buttons
this.getField("m12").value = cResult;
break;
Thank you all for taking the time to read my post. If you can help, please do.
Whether or not it's for a living, it will be well worth your time to learn the basic concepts behind writing this code.
The answer to your question is to change those two lines of code to this:
if (app.alert("Enter unique Proposal Number:\n\n" + cResult, 0, 3)==4)
this.getField("m12").value = cResult;
Copy link to clipboard
Copied
Have a look at the return value of app.alert for each one of the buttons, and then add an if-condition so that the next line is only executed if Yes is clicked (assuming that's what you want to do).
Copy link to clipboard
Copied
Thanks for the reply. The only thing is, I don't know enough about javascript to do what you're suggesting, I'm still a novice. What is an "if-condition"? I would gladly insert it if I had one. What would it look like? Is there a reference that I could use to better understand?
Copy link to clipboard
Copied
This is a very basic thing. If you're going to be writing code in JS then you must be familiar with the basic syntax of the language.
How were you able to come up with the code above (which actually includes an if-condition), if you don't know what it is?
Copy link to clipboard
Copied
I found the code on the user forum. I cut it and pasted it into my Acrobat program just to see what it would do. Just trying things that look interesting. And it worked. I don't plan on writing javascript for a living, I'm just playing around with different things for myself. I looked through the script above and I still don't know what an "if-condition" is or how to write the correct code to make it do what I'd like. I just thought to add the option to not apply the random number.
Copy link to clipboard
Copied
Whether or not it's for a living, it will be well worth your time to learn the basic concepts behind writing this code.
The answer to your question is to change those two lines of code to this:
if (app.alert("Enter unique Proposal Number:\n\n" + cResult, 0, 3)==4)
this.getField("m12").value = cResult;
Copy link to clipboard
Copied
Thanks for the help. It does seem basic as you say. The code is almost the same except for the "if(", and the ")". I will now study it to better understand what an "if-condition" is. I'm not sure what the "==4" is or does.
Copy link to clipboard
Copied
Like many other functions, the alert method returns a value. If the user clicks "Yes" that value is 4.
This is all documented in the Acroabt JS API Reference.
Copy link to clipboard
Copied
Take a look here for some pointers about view to learn to Program in JavaScript for Acrobat: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC
Find more inspiration, events, and resources on the new Adobe Community
Explore Now