5 Replies Latest reply on Nov 9, 2012 12:42 PM by George_Johnson

    Baffling Problem with Acrobat Javascript


      I'm hoping someone can help me. I've tried for hours and hours to find what the trouble is but just can't find a solution.


      The following code works, however, this is the baffling part. I need to edit / update the code but the problem occurs when I make ANY changes to the code - even indenting a line. You will notice that the code is not indented and poorly formatted - that's because when I had the code indented, I repeatedly got a "SyntaxError: missing } in compound statement"


      I am completely baffled as to why sometimes I get the SyntaxError and sometimes not. I swear there are instances where the code worked one time and then another time causes a SyntaxError.


      Tried using Adobe Javascript Debugger, tried running code through jslint.com, tried creating a new PDF file and placing this code in that file - still getting SyntaxError if I change or indent the code. The only thing I haven't tried yet is trying it out on the Windows version.


      I'm using Acrobat Pro 10.1.3 (Creative cloud) on a Mac OS X Lion 10.7.4


      Any help would be really appreciated!



      var trackErrors = 0;

      var errorMessage = 'Please complete all of the required fields';

      if (this.getField('1A').value.length === 0) { errorMessage += ' A Wall Height '; trackErrors++; }

      if (this.getField('1B').value.length === 0) { if (trackErrors === 1) { errorMessage += ', B Wall Length '; } else { errorMessage += ' B: Wall Length '; trackErrors++; } }

      if (this.getField('1C').value.length === 0) { if (trackErrors === 1) { errorMessage += ', C Spacing '; } else { errorMessage += ' C: Spacing '; trackErrors++; } }

      if (this.getField('1-4-radio').value === 'Off') { if (trackErrors === 1) { errorMessage += ' and Material Type - Fabric or Laminate.'; } else { errorMessage += ' Mat type'; trackErrors++; } }

      if (trackErrors === 0) {

      var x, y, v, z, kits = '';

      var flag = 0;

      this.getField('1D').value = this.getField('1B').value / 2;

      this.getField('1E').value = this.getField('1D').value / this.getField('1C').value;

      this.getField('1F').value = Math.floor(this.getField('1E').value);

      this.getField('1G').value = this.getField('1F').value + (this.getField('1F').value - 1);

      this.getField('1H').value = this.getField('1A').value * this.getField('1G').value;

      var materialType = this.getField('1-4-radio').value;

      switch (materialType)


      case 'F':

      x = this.getField('1H').value / 32;

      y = Math.ceil(x);

      this.getField('bw-kits-required').value = y + " Kit Required";


      case 'L':

      x = this.getField('1H').value / 128;

      y = this.getField('1H').value / 32;

      if (flag === 0) {

      if (x === Math.round(x)) { kits += x + ' kit '; flag++; }

      if (y === Math.round(y)) { kits += y + ' kit '; flag++; }

      v = Math.floor(x);

      x -= v;

      if (v > 0) { if (x < 0.75) { kits += v + ' kit'; v = Math.ceil((x * 128) / 32); kits += ' and ' + v + ' kit'; } else if (x >= 0.75) { kits += (v + 1) + ' kit '; } } else { kits += Math.ceil(y) + ' kit'; }


      this.getField('bw-kits-required').value = kits;



      } else { app.alert(errorMessage); }

        • 1. Re: Baffling Problem with Acrobat Javascript
          George_Johnson MVP & Adobe Community Professional

          I don't get the same thing in Acrobat 9 on Windows XP. It would be interesting to see what happens if you copy what you posted here on the forum and use it.



          I would suggest changing code like:


          if (this.getField('1A').value.length === 0)



          To something like this:


          if (this.getField('1A').valueAsString.length === 0)



          If you use the value property and the field value is numeric (or "true" or "false"), it won't have a length property because the type of the value isn't "string", so it will be undefined. Since the special value of undefined is not equal to zero the code works, but it's a bit misleading. It could be further refined to just:


          if (!getField('1A').valueAsString)

          • 2. Re: Baffling Problem with Acrobat Javascript
            ptkforcbm Level 1

            I had no problems on 3 different versions of Adobe Acrobat Pro for Windows. Therefore, I assume the problem lies with Acrobat Pro for the Mac. We've already decided to dump Acrobat Pro for Mac and setup a virtual machine with Windows and edit our scripted PDFs that way - at least until Adobe fixes issues with their Mac version - it never dawned on me that it would be a Mac version problem. I guess I'll be a lot more suspicious going forward. Thanks George for confirming it worked in Acrobat 9!

            • 3. Re: Baffling Problem with Acrobat Javascript
              MarkWalsh Level 4

              I rarely work on PDFs on my mac; the application is very painful to work in (if I'm working with form fields, it does multiple screen redraws whenever I edit the fields, among other things)


              The javascript editor sucks on all platforms, it's buggy and featureless. When I do need to edit code, I have set TextWrangler http://www.barebones.com/products/TextWrangler/ as my editing tool on Mac, on the PC I often use ExtendScript (which, depending on the version, has it's own issues). One of the few issues I have with using an external editor (which is effectively necessary) is that I find, at least on the Mac, that closing the document when finished editing isn't enough, I need to completely quit the editor application. Can't recall if this the same on PC or not.

              • 4. Re: Baffling Problem with Acrobat Javascript
                Mark D Watkins

                Mac Acrobat 10.1.4 on Lion 10.7.5

                Having the same problem. Whether I make changes in the built in Javascript editor, use an external editor, or paste from external editor, the script validation seems be parsing a limited number of characters, at which point it generates an error. If I edit the code down to something under 40 full lines of text, it will allow a save. I can create individual functions and link them together it works, so its not the length of the script itself, just the parsing error on save. If I take that same working code, using the Edit All Javascripts window and try to copy/paste it, I once again get the error.

                Clearly a serious bug that needs to be fixed. Meanwhile the project is stalled as there is no workaround other than Windows version.

                • 5. Re: Baffling Problem with Acrobat Javascript
                  George_Johnson MVP & Adobe Community Professional

                  If you can, try Acrobat 11. There were a number of improvements with the console/debugger, so it may have already been fixed. There are a number of other form authoring related improvements as well (tab order especially) that make it appealing. I don't expect Acrobat 10 will be updated to change this.