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

Scripting dynamic combo boxes

Community Beginner ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

Hello. I am trying to script 2 to 3 comboboxes. The first box would have multiple choices of types of pipe threads I make and each successive combo box would be narrowed by the previous choice.  

I feel there is some type of security issue or I am not running the code correctly. The code works as it should and then it does not. Just looking for some pointers. I am using Acrobat dc and have javascript enabled. Here is an example of what I am struggling with. I will be adding more switch statements with if conditions.  The reason I think my setup is the problem is because I have followed tutorials to the T and am not getting the output I expect or any at times, no errors just nothing. Should I not be checking for functionality with "preview"?  I believe I am working with an interactive pdf.

Any advice is appreciated Thanks

var threadType = this.getField("Dropdownbox1");
var threadDiameter = this.getField("Dropdownbox2");
var weightPerFoot = this.getField(Dropdownbox3);

if (event.willCommit)
    switch (event.value) {
        case "Blue":
            threadDiameter.setItems["2.375", "2.875", "3.50", "4.50", "5.50", "7.00", "9.625"];
            break;
    }

 

TOPICS
Acrobat SDK and JavaScript

Views

2.2K

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 ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

What output does you expect?

Votes

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 Beginner ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

I would like each successive combo box  (Dropdown2), (Dropdown3) filter my choices based on previous selection. I was hoping I could script (Dropdown1) via custom keyscript with one switch/case statement to control/populate  (Dropdown2) and (Dropdown3). I will add unfinished rough pdf

Votes

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 ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

Where did you place the code? Can you share the file?

Votes

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 Beginner ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

I have been putting it my code in the Dropdownbox1 Custom Key Script. Here is a rough idea of where I am. I havent moved forward because I havent been able to get consistant results yet with where I am.

 

It seems like One Drive keeps messing up all my file locations and permissions.  I wonder if that is affecting the functionality.

Votes

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
LEGEND ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

When you're testing a PDF form it's vital to open the JavaScript console. It won't pop up a box telling you there is an error in your script, it all goes to the console. When I choose Blue the console says "TypeError: threadDiameter is null -- 5:AcroForm:Dropdown1:Keystroke"

Votes

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 Beginner ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

 

Thank you for taking the time to reply. Would the proper way to test the code be to make code changes then close out combobox wizard,  then save file, then enter the "JavaScript" acrobat workspace and instantiate code via. Debugger button.

I am getting the same error "TypeError: threadDiameter is null".

Votes

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
LEGEND ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

I'm not entirely sure what the Combobox wizard is, I've never used it, and I don't recognise a JavaScript workspace, just a window. But when I'm using form tools...

1. I keep the JavaScript console open full time - though it can be closed if there isn't room on screen.

2. I exit Prepare Form using the Close button on the toolbar.

The error is giving you the line number with the error. In this case the error can be traced to one of the lines above, you are using getField with the wrong field names. So nothing else will work. However, using getField with the wrong field name doesn't give an error at the time - it is documented that it returns null in this case. The actual error detected is trying to do something with that null value. Careful programmers will add a check after each line that might fail, but it's a lot more work.

Votes

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 ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

The proper way to test this kind of code is to use it. If the code is triggered by changing the value of a drop-down field, then change the field's value, and then see if there are any errors in the JS Console, and if not, whether the code does what you expect it to do.

Votes

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 ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

You use:

var threadType = this.getField("Dropdownbox1");
var threadDiameter = this.getField("Dropdownbox2");
var weightPerFoot = this.getField("Dropdownbox3");

There doesn't exists any field with this names.

Votes

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 ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

As noted by the other posters, your first issue is that the field names are incorrect.

 

Next, and this is a trivial point, there is no reason to get the field object for the field the script is operating in, i.e. "Dropdown1".   Since this field is the target of the script, it's field object is "event.target".  

 

Next, all of the fields need the "Commit selected value immediately" option checked. Then you can use the "Commit" event. 

 

And finally, change how this line is used 
if (event.changeEx!=0) threadDiameter.insertItemAt("- Thread Diameter -", 0);

 

Here's a quick rewrite of the code (it assumes that the commit option is checked):

var threadDiameter = this.getField("Dropdown2");
if (event.willCommit) {
    if(!event.value || (event.value == ""))
       threadDiameter.setItems(["- Thread Diameter -"]);
    else{
       switch (event.value) {
		case "Blue":
			threadDiameter.setItems(["2.375", "2.875", "3.50", "4.50", "5.50", "7.00", "9.625"]);
			break;
		case "563":
			threadDiameter.setItems(["2.875", "3.50", "4.50", "5.50", "6.00", "7.625"]);
			break;
		// etc.
       }
    }
}


 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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 ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

Hi,

If I understood correctly, place this script in custom keystroke script of the Dropdownbox1 field:

var threadType = this.getField("Dropdownbox1");
var threadDiameter = this.getField("Dropdownbox2");
var weightPerFoot = this.getField("Dropdownbox3");
if (!event.willCommit) {
	threadDiameter.clearItems();
	switch (event.changeEx) {
		case "Blue":
			threadDiameter.setItems(["2.375", "2.875", "3.50", "4.50", "5.50", "7.00", "9.625"]);
			break;
		case "Red":
			threadDiameter.setItems(["1.375", "2.875", "3.50", "4.50", "5.50", "6.00", "7.625"]);
			break;
		// etc.
	}
	if (event.changeEx!=0) threadDiameter.insertItemAt("- Thread Diameter -", 0);
}

@+

Votes

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 Beginner ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

Thank you. I am not having succsess with the code installed in "custom key script" DropdownBox1.

I noticed the file and some parent files were read only and they keep being saved in some onedrive folder. It seems like this may be part of my problem. I am now right clicking and changing file to "Always Keep on This Device".  Still not getting desired outcome. Thanks

Votes

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 ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

If you want it to work, you have to check that the field names and data of the files and the script match.

Capture_d’écran_2022-06-24_à_16_26_20.png

@+

Votes

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 Beginner ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

Thank you for all of the information, it has been very helpful. I have been enjoying working through my project and trying to make things functional. I relize that I am just learning this workflow and Im sure my coding is not the best.  Currently I am trying to populate a textBox with a calculated var derived from the Sum of two vars, I can console the the calculated var and I get my desired number but if  I console same var with .value it is undefined. I cannot .setItems to the textbox. Getting "invalid argument type". I can access the information I need via console but cannot implement/assign it to "control"/textbox within my code. Any suggestions are appreciated. I have attatched a screenshot with failing path highlighted. Starting where I am setting up a var =  .getField  -to-   var.setItems of control/textBox that I am trying to populate(which fails to "invalid argument type"). highlighted.

Thanks very much for any advice

Votes

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 Beginner ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

Here is a the .pdf

Votes

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 ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

Hi,

A real code would be better than a screenshot...

@+

Votes

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 Beginner ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

var weight_per_foot = this.getField("weightPerFt");
var thread_diameter = this.getField("threadDiameter");
var thread_diameter_A = this.getField("threadDiameter");
var torque_make_up_length = this.getField("torqueMakeUpLength");
var make_up_comp_length = this.getField("makeUpCompLength"); // need to populate with var compensated_joint_length 
var joint_length = this.getField("jointLength");
var compensated_joint_length = make_up_comp_length.value + joint_length.value;

//Tests
// torque_make_up_length + joint_length

console.println("torque_make_up_length + joint_length");
console.println(compensated_joint_length);

var SELECT = event.value;


if (SELECT == "563") {

    thread_diameter.setItems([thread_diameter_A.value, "2.375", "2.875", "3.50", "4.00", "4.50", "5.50", "6.625", "7.00", "9.625"]);

    //Tests
    console.println("from 563 thread_diameter_A");
    console.println(thread_diameter_A.value);


    weight_per_foot.setItems(["4.60(WT. 0.190 in)", "6.40(WT. 0.217 in)", "9.20(WT. 0.254 in)", "11.00(WT. 0.262 in)", "11.60(WT. 0.250 in)",
        "12.60(WT. 0.271 in)", "13.20(WT. 0.330 in)",
        "14.00(WT. 0.244 in)", "15.00(WT. 0.296 in)", "15.20(WT. 0.337 in)", "15.50(WT. 0.275 in)", "17.00(WT. 0.304 in)", "18.00(WT. 0.362 in)",
        "20.00(WT. 0.288 in)", "24.00(WT. 0.352 in)",
        "24.10(WT. 0.500 in)", "6.40(WT. 0.217 in)", "26.00(WT. 0.362 in)", "29.00(WT. 0.408 in)"
    ]);

} else if (SELECT == "Blue") {
    thread_diameter.setItems([thread_diameter_A.valueAsString, "2.375", "2.875", "3.50", "4.50", "5.50", "7.00", "9.625"]);

    //Tests
    //console.println(thread_diameter_A.value);
    //console.println("from Blue thread_diameter_A");
    console.println(torque_make_up_length.value);

    weight_per_foot.setItems(["4.60(WT. 0.190 in)", "6.40(WT. 0.217 in)", "9.20(WT. 0.254 in)", "12.60(WT. 0.271 in)", "17.00(WT. 0.304 in)",
        "26.00(WT. 0.362 in)", "29.00(WT. 0.408 in)", "29.00(WT. 0.217 in)", "53.50(WT. 0.545 in)"
    ]);
}

// 563 Threads, Diameters, Weights And Wall Thickness
if (thread_diameter_A.value == "2.375" && SELECT == "563") {
    weight_per_foot.setItems(["4.60(WT. 0.190 in)"]);
    torque_make_up_length.value = "3.642";
    //make_up_comp_length.setItems(compensated_joint_length);

    //   |         |          |          |          |          |          |  
    //FAILING	FAILING    FAILING    FAILING    FAILING    FAILING    FAILING                 
    //   V         V          V          V          V          V          V
    ///////////////////////////////////////////////////////////////////
    make_up_comp_length.setItems("compensated_joint_length"); //populate make_up_comp_length textBox
    ///////////////////////////////////////////////////////////////////

    // Test
    console.println("Joint Length Math");
    console.println(compensated_joint_length);

Votes

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
LEGEND ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

make_up_comp_length.setItems("compensated_joint_length")

-- but setItems requires an array. You are passing only a single string, "compensated_joint_length". Not even the name of a variable, but the variable would need to contain an array, which it doesn't.

But you say in your requirement, that "I am trying to populate a textBox with a calculated var ". But setItems is documented as being for a combo box or list box. Which is it? A combo box, a list box, or a textBox? You could also try sharing the PDF you are working on, which isn't what you posted.

Votes

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 Beginner ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

I am trying to populate one text box with one element. Thank you for pointing the plurality out. I guess I thought that all of the Field types were handled in a similar manner and did not assume that a textbox could not display an array of values or vice versa.  I will attatch my pdf. 

Votes

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 ,
Jul 02, 2022 Jul 02, 2022

Copy link to clipboard

Copied

Check the Javascript console for errors.

Votes

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 Beginner ,
Jul 02, 2022 Jul 02, 2022

Copy link to clipboard

Copied

LATEST

Thank you for all the help. Changing my error of trying to .setItems to text box. Replacing 

 make_up_comp_length.setItems("compensated_joint_length"); //populate make_up_comp_length textBox

to

 make_up_comp_length.value = compensated_joint_length;

 has given me the desired output to textbox.

Thanks again

Votes

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