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

Need help with this script. Take a look.

Participant ,
Feb 11, 2017 Feb 11, 2017

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.

TOPICS
Acrobat SDK and JavaScript , Windows
534
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 , Feb 11, 2017 Feb 11, 2017

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;

Translate
Community Expert ,
Feb 11, 2017 Feb 11, 2017

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

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
Participant ,
Feb 11, 2017 Feb 11, 2017

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?

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 ,
Feb 11, 2017 Feb 11, 2017

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?

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
Participant ,
Feb 11, 2017 Feb 11, 2017

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.

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 ,
Feb 11, 2017 Feb 11, 2017

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;

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
Participant ,
Feb 12, 2017 Feb 12, 2017

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.

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 ,
Feb 12, 2017 Feb 12, 2017
LATEST

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.

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 ,
Feb 11, 2017 Feb 11, 2017

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

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