3 Replies Latest reply on Sep 19, 2013 3:48 AM by Bernd Alheit

    Field Formatting on open

    T_Stevens

      So here's the problem:

        I have a form with 100+ fields that all needed to be formatted as currency. This form will be used in multiple countries, so I have a button that opens a custom dialog that allows the user to set how many decimal points to use, which currency symbol to use, and where the symbol should be located. That works great. What I would like to do is have the dialog box pop up when the document is opened, but when I call my dialog function on open, the fields don't format.

       

      Here's the code:

       

      var beenOpened;

       

      function docOpened(){

          if(beenOpened != true){

              dialog();

              beenOpened = true;

          }

      }

       

      docOpened();

       

      function dialog()

      {

      // Dialog Definition

       

      var curDlg = {

          initialize: function(dialog) {

              dialog.load({

                  symb:{" $ ": +1, " ¢ ": -2, " £ ": -3, " ¥ ": -4, " ฿ ": -5, " ₡ ": -6, " ₢ ": -7, " ₣ ": -8, " ₤ ": -9, " ₥ ": -10, " ₦ ": -11, " ₧ ": -12, " ₨ ": -13, " ₩ ": -14, " ₪ ": -15, " ₫ ": -16, " € ": -17, " ₭ ": -18, " ₮ ": -19, " ₯ ": -20, " ¤ ": -21, " ₠ ": -22, " ₰ ": -23, " ₱ ": -24

                  }

              })

          },

          dec: "",

          sep: "",

          loc: "",

          sym: "",

          commit: function(dialog) {

              var data = dialog.store();

              var elements = dialog.store()["symb"];

              if (data["num0"]) {

                  this.dec = 0;}

              if (data["num1"]) {

                  this.dec = 1;}

              if (data["num2"]) {

                  this.dec = 2;}

              if (data["sep0"]) {

                  this.sep = 0;}

              if (data["sep1"]) {

                  this.sep = 1;}

              if (data["sep2"]) {

                  this.sep = 2;}

              if (data["sep3"]) {

                  this.sep = 3;}

              if (data["loc0"]) {

                  this.loc = true;}

              if (data["loc1"]) {

                  this.loc = false;}

              if (data["loc2"]) {

                  this.loc = 3;}

              for(var i in elements) {

                  if ( elements[i] > 0 ) {

                  this.sym = i;}

              }

          },

          description: {

              name: "Currency Format", elements: [

                  { type: "cluster", elements: [

                      { name: "Choose Decimal Places:", type: "static_text", },

                      { name: "0", type: "radio", item_id: "num0", group_id: "grp1" },

                      { name: "1", type: "radio", item_id: "num1", group_id: "grp1" },

                      { name: "2", type: "radio", item_id: "num2", group_id: "grp1" }, ] },

                   { type: "cluster", elements: [

                      { name: "Choose separator:", type: "static_text", },

                      { name: "1,234.56", type: "radio", item_id: "sep0", group_id: "grp3" },

                      { name: "1234.56", type: "radio", item_id: "sep1", group_id: "grp3" },

                      { name: "1.234,56", type: "radio", item_id: "sep2", group_id: "grp3" },

                      { name: "1234,56", type: "radio", item_id: "sep3", group_id: "grp3" }, ] },

                  { type: "cluster", elements: [

                      { name: "Choose Symbol Location:", type: "static_text", },

                      { name: "Before", type: "radio", item_id: "loc0", group_id: "grp2" },

                      { name: "After", type: "radio", item_id: "loc1", group_id: "grp2" },

                      { name: "No symbol", type: "radio", item_id: "loc2", group_id: "grp2" }]},

                  { type: "view", align_children: "align_left", elements: [

                      { type: "cluster", name: "Currency symbol", elements: [

                          { type: "static_text", name: "Select symbol you use", font: "default" },

                          { type: "list_box", item_id: "symb", width: 200, height: 400 }]},

                  { type: "view", elements: [

                      { type: "ok_cancel", }]}

              ]}

      ]}};

       

      // Dialog Activation

       

      app.execDialog(curDlg);

       

      // Formatting

      var myDec = curDlg.dec;

      var myLoc = curDlg.loc;

      var mySym = curDlg.sym;

      var mySep = curDlg.sep;

       

      fieldFormat(myDec,mySep,mySym,myLoc);

       

      }

       

       

      function fieldFormat(myDec,mySep,mySym,myLoc)

      {

      //create field arrays

      var totArray = [];

      var amntArray = [];

      var f;

       

      var temp = "";

      for( var i = 1; i < 32; i++){

      temp = "Amounts-a." + i;

      amntArray.push(temp);

      }

      for( var i = 1; i < 32; i++){

      temp = "Amounts-b." + i;

      amntArray.push(temp);

      }

      for( var i = 1; i < 32; i++){

      temp = "Amounts-c." + i;

      amntArray.push(temp);

      }

      for( var i = 1; i < 32; i++){

      temp = "Total." + i;

      totArray.push(temp);

      }

       

      //change field formats

      for(var i = 0; i < amntArray.length; i++){

          f = this.getField(amntArray[i]);

          f.setAction("Format", "AFNumber_Format\(myDec, mySep, 0, 0, mySym, myLoc\);");

          f.setAction("Keystroke", "AFNumber_Keystroke\(myDec, mySep, 0, 0, mySym, myLoc\);");

      }

      for(var i = 0; i < totArray.length; i++){

          f = this.getField(totArray[i]);

          f.setAction("Format", "AFNumber_Format\(myDec, mySep, 0, 0, mySym, myLoc\);");

          f.setAction("Keystroke", "AFNumber_Keystroke\(myDec, mySep, 0, 0, mySym, myLoc\);");

      }

       

      }

       

      Any ideas on why this works as a button, but not as an on open document level script?

        • 1. Re: Field Formatting on open
          gkaiseril MVP & Adobe Community Professional

          What code are you using for your button?

           

          What code calls the function as a document level script?

           

          You have only posted the code that defines the function.

          • 2. Re: Field Formatting on open
            T_Stevens Level 1

            Sorry I wasn't very clear. I have three document level scripts. The first is docOpened, which contains the first piece of code above through the docOpened() function call. The second script contains just the dialog function, and the third contains just the fieldFormat function.

             

            My button was scripted without any function calls. It's actually just the guts of the dialog and fieldFormat functions back to back and is run as a mouse up action.

             

            I created the document level scripts after I had my button working in an attempt to have the user set the field formats when the document opened. I essentially copied and pasted my button script into the dialog and fieldFormat functions. Then I saved, closed, and reopened the document to test it. The dialog box appeared as expected, but the fields weren't formatted.

             

            I just created a test button that calls the dialog and fieldFormat functions. The dialog box appears just fine, but the fields don't take the formatting. I assume that means there is a problem with either the fieldFormat function, or the way I'm calling that function, but I still can't see where the error could be.

             

            Thanks for the help.

            • 3. Re: Field Formatting on open
              Bernd Alheit Adobe Community Professional & MVP

              Does the variables myDec, mySep, mySym, and myLoc have values when you look at it in the javascript debugger?