Search code examples
javascriptvariablesdialogadobeacrobat

Javascript in Acrobat - Can't pass values out of dialog box


I'm having difficulty working with a custom dialog box in Adobe Acrobat.

My objective is to have a dialog box appear when you click a button in the toolbar at the top. The dialog box will contain an editable text field, and a list_box. Once you enter text, make a selection from the list box, and click OK, the values get passed to a text field on the PDF document. Right now I'm having their values return to the console instead.

Each time I try to run the code, both variables (one for the text field entry, one for the list box selection) both come back undefined. I'm very new to javascript in Acrobat, so this project is slowly coming together like a Frankenstein consisting of different code snippets from the internet, lol.

Any insight would be greatly appreciated!

var timeDialog = {
    initialize: function(dialog) {this.loadDefaults(dialog);},
        commit: function(dialog) { 
            var timeEntry = dialog.store()["tetb"]
            var techEntry = dialog.store()["tnlb"]
          },
        loadDefaults: function (dialog) {
            dialog.load({
                "tnlb":
                {
                    "Jimmy John": +2,
                    "Papa John": +1,
                    "Great Gatsby": +0
                }
            })
        },
        description: 
        {
            name: "Time Dedicated", 
            elements: 
            [
                { 
                    type: "view",  
                    elements: 
                    [
                        {
                            type: "cluster",
                            elements:
                            [
                                {
                                    name: "Time spent on Request (minutes):", 
                                    type: "static_text"
                                },
                                {
                                    type: "edit_text",
                                    item_id: "tetb",
                                    char_width: 5
                                },
                                {
                                    name: "Technician:", 
                                    type: "static_text"
                                },
                                {
                                    item_id: "tnlb", 
                                    type: "list_box",
                                    width: 200,
                                    height: 60
                                }
                            ]
                        },
                        {
                            type: "ok_cancel"
                        }
                    ]
                }
            ]
        }
    }

if( "ok" == app.execDialog(timeDialog)) { 
    var today = new Date();
    var time = today.getHours() + "." + today.getMinutes() + "." + today.getSeconds();
    var TEMP_FIELD_NAME = timeDialog.timeEntry
    var textValue = timeDialog.techEntry;
    console.println(TEMP_FIELD_NAME); 
    console.println(textValue); 
}

Thank you!


Solution

  • Take a look at the new commit function I wrote for you. List items get stored as the list where the one with a positive index value is the selected item. You can then access the value selected.

    var timeDialog = {
        initialize: function (dialog) { this.loadDefaults(dialog); },
        commit: function (dialog) {
            this.timeEntry = dialog.store()["tetb"];
            var items = dialog.store()["tnlb"]
            for (var item in items) {
                if (items[item] > 0) {
                    this.techEntry = item;
                    break;
                }
            }
        },
        loadDefaults: function (dialog) {
            dialog.load({
                "tnlb":
                {
                    "Jimmy John": +2,
                    "Papa John": +1,
                    "Great Gatsby": +0
                }
            })
        },
        description:
        {
            name: "Time Dedicated",
            elements:
                [
                    {
                        type: "view",
                        elements:
                            [
                                {
                                    type: "cluster",
                                    elements:
                                        [
                                            {
                                                name: "Time spent on Request (minutes):",
                                                type: "static_text"
                                            },
                                            {
                                                type: "edit_text",
                                                item_id: "tetb",
                                                char_width: 5
                                            },
                                            {
                                                name: "Technician:",
                                                type: "static_text"
                                            },
                                            {
                                                item_id: "tnlb",
                                                type: "list_box",
                                                width: 200,
                                                height: 60
                                            }
                                        ]
                                },
                                {
                                    type: "ok_cancel"
                                }
                            ]
                    }
                ]
        }
    }
    
    if ("ok" == app.execDialog(timeDialog)) {
        console.println(timeDialog.timeEntry);
        console.println(timeDialog.techEntry); 
    }