Skip to main content
Inspiring
July 25, 2020
Answered

help modify the script

  • July 25, 2020
  • 1 reply
  • 941 views

Could someone possibly help me and modify code for me?

I would need to change from  "edit_text" to "popup" with selections t1,t2,t3 and "ok" button to write selections to text field. I'm trying this whole day and just can't figure it out.

 

var dlg =  {
   commit: function(dialog)
   {
      var data = dialog.store();
      this.strName = data["usnm"];
   },
butn: function(dialog){
   dialog.end("butn");
},
    description:  { name: "", elements: [
  {
type: "static_text",
name: "",
char_width: 15,
alignment: "align_center",
font: "dialog",
bold: true,
},      
{ type: "cluster", name: "",elements: [
    	{ name: "", type: "static_text", },           	
    ] },
{ type: "cluster", align_children: "align_row", elements: [
{ name: "", type: "static_text", },
{ item_id: "usnm", type: "edit_text", char_width: 4, },
{
type: "button",
item_id: "butn",
name: "Reset"
}
]},
{ type: "ok_cancel",
        ok_name: "Ok",
        cancel_name: "Cancel",
        },
] }
 };

var dialogReturnValue = app.execDialog(dlg);
if ("ok" == dialogReturnValue) {
this.getField("field").value = dlg.strName;
} else if ("butn" == dialogReturnValue) {
this.getField("field").value = "";
}

 

 

This topic has been closed for replies.
Correct answer Thom Parker

Hi try67,  I don't know how to add selection to that popup(example:text1,text2...etc) and also ok button to write my selection to textfield.

EDIT:

I managed to make some progress but when I press ok button in my textfield it says "undefined" can you take a look and let me know what I need to change to make "ok" write my choice to textfield.

var dlg =  {
  initialize: function(dialog) {
this.loadDefaults(dialog);
},
 commit: function(dialog)
   {
      var data = dialog.store()["usnm"];
      this.strName = data["usnm"];
   },
other: function(dialog){
   dialog.end("other");
},
loadDefaults: function (dialog) {
dialog.load({
usnm:
{
"1": 1,
"2": -1,
"3": -2
}
})
},
    description:  { name: "text", elements: [
      {
type: "static_text",
name: "CUSTOM TITLE",
char_width: 15,
alignment: "align_center",
font: "dialog",
bold: true,
},  
{ type: "cluster", name: "text",elements: [
    	{ name: "text",
 type: "static_text",
 char_height: 4 },           	
    ] },
{ type: "cluster", align_children: "align_row", elements: [
{ name: "text", type: "static_text", },
{ item_id: "usnm", type: "popup", char_width: 20, },
]},
{ type: "ok_cancel_other",
        ok_name: "Ok",
        cancel_name: "Cancel",
        other_name: "Reset" },
] }
 };

var dialogReturnValue = app.execDialog(dlg);
if ("ok" == dialogReturnValue) {
this.getField("field").value = dlg.strName;
} else if ("other" == dialogReturnValue) {
this.getField("field").value = "0";
}

You've aleady learned that a list field on a dialog is populated with a object. Where the item names are the text that appears in list, and the item value is "1" for a selected item and "-1" for an unselected item. 

Now, getting a value from a list is exactly the same. 

 

dialog.store()["usnm"];

 

This code returns an object. To the get selected item, the code needs to examine each item in the object to find the one that has a value of "1".  

Here's the code you need in the "commit" function

 

var oList = dialog.store()["usnm"];
this.strName = "";
for(var nm in oList)
{
    if(Number(oList[nm]) > 0)
    {
       this.strName = nm;
       break;
    }
}

 

 On a separate note, there is no need to have a "loadDefaults" function. It just over complicates the code. 

 

You can find out more about programming Acrobat JavaScript dialogs, and also find a drag and drop tool that created the dialog code for you here:

https://www.pdfscripting.com/public/ACRODIALOGS-OVERVIEW.cfm

 

1 reply

ls_rbls
Community Expert
Community Expert
July 26, 2020

What errors are you getting?

 

Open the console and see what messages are posted.

 

I am learning Acrobat javascript and your script seems a bit too advanced for my current level.

 

However, I can't help but notice a bunch of comas "," that in my humble opinion, appear to be unnecessary (or wrong) with some of the parameters that end with the closing curly brace "}".

 

For example see the following lines used in your script:

{ type: "cluster", name: "",elements: [
{ name: "", type: "static_text", },
] },
{ type: "cluster", align_children: "align_row", elements: [
{ name: "", type: "static_text", },

 

Can you confirm if this is how they should be expressed:

 

 

 

{ type: "cluster", name: "",elements: [
    	{ name: "", type: "static_text"},           	
    ] },
{ type: "cluster", align_children: "align_row", elements: [
{ name: "", type: "static_text"},

 

Asim123Author
Inspiring
July 26, 2020

Inside commas goes text which I deleted for this post,code is working fine I just want to change it from input field to popup selection.

try67
Community Expert
Community Expert
July 26, 2020

Change:

{ item_id: "usnm", type: "edit_text", char_width: 4, },

To:

{ item_id: "usnm", type: "popup", char_width: 4, },