23 Replies Latest reply on Oct 27, 2016 5:17 PM by TavC

    Unterminated String Literal..?

    dremani

      I am receiving an error message "unterminated string literal" in my custom calculation script and not sure why or how to fix it.

       

      There error seems to occur on one of the last two lines on the code below...my code is about 4 times this length, but I can only make it to this point when the error occurs...the problem is not with the particular code, because if I remove some code before it, it works fine....maybe it has to do with the legth of the code? Are there any limits or restrictions that may be affecting this?

       

       

      if (this.getField("TICKET1").value =="-") {event.value = 0.00}

       

      if (this.getField("TICKET1").value =="GENERAL (2011-2012 Season TIcket)" && this.getField("TYPE1").value == "ADULT") {event.value = 814.00;}
      if (this.getField("TICKET1").value =="GENERAL (2011-2012 Season TIcket)" && this.getField("TYPE1").value == "STU/SEN") {event.value = 740.00;}
      if (this.getField("TICKET1").value =="GENERAL (2011-2012 Season TIcket)" && this.getField("TYPE1").value == "CHILD") {event.value = 555.00;}

       

      if (this.getField("TICKET1").value =="GENERAL (2011-2012 Season TIcket Early Bird)" && this.getField("TYPE1").value == "ADULT") {event.value = 580.00;}
      if (this.getField("TICKET1").value =="GENERAL (2011-2012 Season TIcket Early Bird)" && this.getField("TYPE1").value == "STU/SEN") {event.value = 515.00;}
      if (this.getField("TICKET1").value =="GENERAL (2011-2012 Season TIcket Early Bird)" && this.getField("TYPE1").value == "CHILD") {event.value = 366.00;}

       

      if (this.getField("TICKET1").value =="GENERAL (2010-2011 Playoffs - 16 game pack)" && this.getField("TYPE1").value == "ADULT") {event.value = 384.00;}
      if (this.getField("TICKET1").value =="GENERAL (2010-2011 Playoffs - 16 game pack)" && this.getField("TYPE1").value == "STU/SEN") {event.value = 352.00;}
      if (this.getField("TICKET1").value =="GENERAL (2010-2011 Playoffs - 16 game pack)" && this.getField("TYPE1").value == "CHILD") {event.value = 272.00;}

       

      if (this.getField("TICKET1").value =="GENERAL (2010-2011 Playoffs - Round 1)" && this.getField("TYPE1").value == "ADULT") {event.value = 88.00;}
      if (this.getField("TICKET1").value =="GENERAL (2010-2011 Playoffs - Round 1)" && this.getField("TYPE1").value == "STU/SEN") {event.value = 80.00;}
      if (this.getField("TICKET1").value =="GENERAL (2010-2011 Playoffs - Round 1)" && this.getField("TYPE1").value == "CHILD") {event.value = 60.00;}

       

      if (this.getField("TICKET1").value =="GENERAL (2010-2011 Playoffs - Round 2)" && this.getField("TYPE1").value == "ADULT") {event.value = 96.00;}
      if (this.getField("TICKET1").value =="GENERAL (2010-2011 Playoffs - Round 2)" && this.getField("TYPE1").value == "STU/SEN") {event.value = 88.00;}
      if (this.getField("TICKET1").value =="GENERAL (2010-2011 Playoffs - Round 2)" && this.getField("TYPE1").value == "CHILD") {event.value = 68.00;}

        • 1. Re: Unterminated String Literal..?
          try67 MVP & Adobe Community Professional

          The error message should specify in which line the error occurred. You

          should focus on that line and check whether your forgot a double-quote or

          parenthesis or something like that.

          The code you posted here compiles fine, as far as I can see.

          • 2. Re: Unterminated String Literal..?
            dremani Level 1

            They error occurs on Line 21, and it doesn't seem to matter which code is in that position..I move the code around, and still the error

            occurs on Line 21. Any thoughts?


            • 3. Re: Unterminated String Literal..?
              try67 MVP & Adobe Community Professional

              That's the last line of the code. It probably means you forgot a closing

              bracket somewhere. Try either debugging by adding break-points, or by

              running just parts of the code and thus locating the problem.

              • 4. Re: Unterminated String Literal..?
                George_Johnson MVP & Adobe Community Professional

                I don't see the problem looking at the code, but you might want to simplify it to something like the following, which should make it easier to maintain:

                 

                (function () {

                 

                // Get the input field values
                var v1 = getField("TICKET1").valueAsString;
                var v2 = getField("TYPE1").valueAsString;

                 

                // Price is zero if nothing is selected
                if (v1 === "-") {
                    event.value = 0;
                    return;
                }

                 

                // Concatenate two values, separated by the "|" character
                var s = v1 + "|" + v2;

                 

                // Object to associate description to price
                var oPrice = {};
                oPrice["GENERAL (2011-2012 Season TIcket)|ADULT"] = 814;
                oPrice["GENERAL (2011-2012 Season TIcket)|STU/SEN"] = 740;
                // Add the other possibilites here
                oPrice["GENERAL (2010-2011 Playoffs - Round 2)|CHILD"] = 68;

                 

                // Set this field value
                event.value = oPrice[s];

                 

                })();

                • 5. Re: Unterminated String Literal..?
                  gkaiseril MVP & Adobe Community Professional

                  Use a matrix array to look up the price for a ticket option and purchaser type:

                   

                  // define array of ticket package and type of purchaser
                  // price array [ticket, type]
                  var nPrice = new Array(
                  [0, 0, 0],
                  [0, 814, 740, 555],
                  [0, 580, 515, 346],
                  [0, 684, 352, 272],
                  [0, 88, 80, 60],
                  [0, 96, 88, 68]
                  );

                  // assign element index number for ticket package
                  var iTicket;
                  switch(this.getField("TICKET1").value) {
                     case "GENERAL (2011-2012 Season TIcket)":
                        iTicket = 1;
                     break;
                     case "GENERAL (2011-2012 Season TIcket Early Bird)":
                        iTicket = 2;
                     break;
                     case "GENERAL (2010-2011 Playoffs - 16 game pack)":
                        iTicket = 3;
                     break;
                     case "GENERAL (2010-2011 Playoffs - Round 1)":
                        iTicket = 4;
                     break;
                     case "GENERAL (2010-2011 Playoffs - Round 2)":
                        iTicket = 5;
                     break;
                     default:
                        iTicket = 0;
                     break;
                     }

                  // assign element index number for purchaser type
                  var iType;
                  switch(this.getField("TYPE1").value) {
                     case "ADULT":
                        iType = 1;
                     break;
                     case "STU/SEN":
                        iType = 2;
                     break;
                     case "CHILD":
                        iType = 3;
                     break;
                     default:
                        iType = 0;
                     break;
                     }
                  // get price for iTicket and iType
                  event.value =  nPrice[iTicket][iType];

                  If you use the optional export values the code becomes much simpler, just the array and lookup by ticket and type.

                  • 6. Re: Unterminated String Literal..?
                    alanomaly Level 1

                    Just to mention I'm having a similar problem in Acrobat X on Mac of mysterious errors which alternate seemingly randomly between "unterminated string literal", or "missing ) in parenthetical", or "missing } in compound statement", or "missing : after property id".There seems to be some kind of bug here, or at the very least, non-standard behaviour.

                     

                    They always occur at the end of the code and seem unrelated to what the code actually says.

                     

                    Here's one example, giving "unterminated string literal" on the this.extractPages... line:

                     

                    if(isInt(pagesPerRecord)) {

                        for( var i = 0; i < this.numPages; i+pagesPerRecord ) {

                            var filename = csvData.getRowAndColumn(i, 'filename') + '.pdf';

                            var end = i+pagesPerRecord-1;

                            app.alert(filename);

                            filename = filename ? filename : 'file-'+((i+pagesPerRecord)/pagesPerRecord)+'.pdf';

                            this.extractPages({ nStart: i, nEnd: end, icPath: filename });

                        }

                    }

                     

                    If I make a minor, cosmetic change so the code reads like this (not defining end as a variable), the error changes to "missing ) in parenthetical":

                     

                    if(isInt(pagesPerRecord)) {

                        for( var i = 0; i < this.numPages; i+pagesPerRecord ) {

                            var filename = csvData.getRowAndColumn(i, 'filename') + '.pdf';

                            app.alert(filename);

                            filename = filename ? filename : 'file-'+((i+pagesPerRecord)/pagesPerRecord)+'.pdf';

                            this.extractPages({ nStart: i, nEnd: i+pagesPerRecord-1, icPath: filename });

                        }

                    }

                     

                    If I make a minor, cosmetic change so the code reads like this (using a different form of if logic on the check for a filename), the error changes to "missing } in compound statement":

                     

                    if(isInt(pagesPerRecord)) {

                        for( var i = 0; i < this.numPages; i+pagesPerRecord ) {

                            var filename = csvData.getRowAndColumn(i, 'filename') + '.pdf';

                            app.alert(filename);

                            filename = filename || 'file-'+((i+pagesPerRecord)/pagesPerRecord)+'.pdf';

                            this.extractPages({ nStart: i, nEnd: i+pagesPerRecord-1, icPath: filename });

                        }

                    }

                     

                    There have been times where I've had errors like this for valid code, and I've deleted and re-typed all the + signs, and it's worked again. Really don't understand this, as the error messages seem unrelated to the code. The rest of the code that ends with this hasn't changed since an earlier version that worked.

                     

                    There are definitely definitely no unclosed brackets, parenthesies or quotes. I've checked manually and with text editor tools (and by copying and pasting directly from an earlier version that worked).

                     

                    I'm sure there's a bug in error reporting or syntax checking that explains the original problem and mine.

                     

                    ---------

                     

                    Edit: Weirder  and weirder, I changed an unrelated line earlier in the code, in the section that is unchanged from an earlier version that worked fine, from:

                     

                    app.alert("5 - "+pagesPerRecord+" pages per record (isInt=="+isInt(pagesPerRecord)+")");

                     

                    ...to...

                     

                    app.alert("5 - "+pagesPerRecord+" pages per record");

                     

                    ...on the basis that another time I had this problem, just deleting lines that involved string concaternation seemed to fix it, and the error message changed yet again to "missing : after property id", while still pointing at the same line (the penultimate }  ). Odd, very odd.

                    • 7. Re: Unterminated String Literal..?
                      Test Screen Name Most Valuable Participant

                      Can't pick up anything from those fragments. Do you have any complete examples with an unaccountable failure?

                      • 8. Re: Unterminated String Literal..?
                        alanomaly Level 1

                        Here's the whole thing as it currently stands. Like the original post, adding or moving lines causes the error message to appear for the same line number even though there is different code there. (e.g. adding and moving the line "var blah="blah";" doesn't change the line number of the error)

                         

                        The ugly long-winded formatting at the end is because a time I had this problem earlier, I got rid of it by reformatting long lines of code into multiple shorter lines.

                         

                        It's currently determined that there is an error on line 68, regardless of what's at line 68

                         

                        ________________________

                         

                        var CSV = function(data) {

                            var _data = data.split('\r\n');

                            app.alert("a "+typeof _data + " ... " + _data.length);

                            if(_data.length <= 1) {

                                _data = data.split('\r');

                                app.alert("b "+typeof _data + " ... " + _data.length);

                            }

                            if(_data.length <= 1) {

                                _data = data.split('\n');

                                app.alert("c "+typeof _data + " ... " + _data.length);

                            }

                            for(var i in _data) {

                                if(_data[i].length > 0) {

                                    console.println(i + ' ' + _data[i]);

                                    _data[i] = _data[i].split(',');

                                }

                            }

                         

                            var _head = _data.shift();

                            return {

                                length: function() {

                                    return _data.length;

                                },

                                adjustedLength: function() {

                                    return _data.length - 1;

                                },

                                getRow: function(row) {

                                    return _data[row];

                                },

                                getRowAndColumn: function(row, col) {

                                    if(typeof col !== 'string') {

                                        return _data[row][col];

                                    } else {

                                        col = col.toLowerCase();

                                        for(var i in _head) {

                                            if(_head[i].toLowerCase() === col) {

                                                return _data[row][i];

                                            }

                                        }

                                    }

                                }

                            };

                        };

                         

                        function isInt(n) {

                           return typeof n === 'number' && n % 1 == 0;

                        }

                         

                        this.importDataObject("CSV Data");

                        var dataObject = this.getDataObjectContents("CSV Data");

                        var csvData = new CSV(util.stringFromStream(dataObject));

                        var pagesPerRecord = this.numPages / csvData.length();

                         

                        if( isInt(pagesPerRecord) ) {

                            for( var i = 0; i < this.numPages; i+pagesPerRecord ) {

                                var end = i+pagesPerRecord-1;

                                var recordIndex = (i+pagesPerRecord)/pagesPerRecord;

                                var filename = csvData.getRowAndColumn(i, 'filename');

                                filename = filename + '.pdf';

                                var blah = "blah";

                                app.alert(filename);

                                if(!filename) {

                                    filename = 'file-'+recordIndex+'.pdf';

                                }

                                var settings = {};

                                settings.nStart = i;

                                settings.nEnd = end;

                                settings.icPath = filename;

                                this.extractPages( settings );

                            }

                        }

                        • 9. Re: Unterminated String Literal..?
                          try67 MVP & Adobe Community Professional

                          I executed the full code and there were no syntax errors.

                           

                          I do notice, though, that sometimes you're using double-quote for strings and sometimes single quotes. I would avoid doing that. Stick with one and it will be easier to locate problems.

                          • 10. Re: Unterminated String Literal..?
                            alanomaly Level 1

                            How did you execute it? I'm running it through an Action under Tools (Acrobat Pro X, Mac, Lion).

                             

                            This seems to confirm my suspicion that it's not the code, but something about how the software is running the code.

                            • 11. Re: Unterminated String Literal..?
                              try67 MVP & Adobe Community Professional

                              From the JS console.

                              • 12. Re: Unterminated String Literal..?
                                alanomaly Level 1

                                Stranger and stranger observations:

                                 

                                • Deleting one of the early app.alert(); lines moves the error message down one line, from the penultimate line to the last line. Deleting a second one of the early app.alert(); lines causes the error message to disappear and the code to run (as if it pushed it off the page...)
                                • Deleting any amount of whitespace lines of code has no such effect
                                • If I delete one of any other lines of code that don't include a { or } - for example col = col.toLowerCase(); then nothing happens - but if I then delete a second other such random line of code - any line of code - the error stops happening and the code runs. It doesn't seem to matter where in the code I delete these lines from.
                                • Restarting Acrobat has no effect
                                • Creating a new action has no effect
                                • Adding lines of code then deleting them - for example adding a few var blah = "blah"; type lines, runnning the code, then deleting the dummy lines - has no effect. It has to be original lines that are deleted.
                                • There is no effect from moving lines of code onto the same line rather than deleting them (i.e. so that there are two closing semicolons on the same line, for example

                                        _data = data.split('\n'); app.alert("c "+typeof _data + " ... " + _data.length);

                                • Switching to an external text editor (in my case, Sublime Text) instead of the basic built in one doesn't improve matters - instead of displaying nonsensical error messages, on save it causes Acrobat to crash. So the problem seems to be in Acrobat, not in Acrobat's built in text editor.

                                 

                                I can't explain this, but it seems if you're stuck with this bug and nothing seems to work, try deleting a few lines of code...

                                 

                                Consider it a sacrifice to the malevolent Acrobat spirits. Like all good malevolent spirits, Acrobat can tell if you're trying to cheat it with an unworthy sacrifice.

                                • 13. Re: Unterminated String Literal..?
                                  alanomaly Level 1

                                  I've finally figured out how to run code in the Acrobat console (on a Mac, it's cmd+J, the enter the code in the console, then run it with ENTER not RETURN)

                                   

                                  It now gives this error:

                                  SyntaxError: syntax error

                                  1:Console:Exec

                                  undefined

                                   

                                  Exact same code as above, copied from the comment above, into Sublime Text to remove formatting (else it gets confused with an encoding error), then into the console.

                                  • 14. Re: Unterminated String Literal..?
                                    Test Screen Name Most Valuable Participant

                                    I put this into an action in Acrobat X, ran the action. Obviously I don't have any useful data objects, but there was certainly no error. I got as far as a request to Save.

                                     

                                    Can you try pasting directly back from your post to Acrobat and see if it's OK? I assume you are editing in an external app, but that shouldn't mess it up unless of course you use Word.

                                     

                                    It may be worth pasting the whole JavaScript console. Could be more clues there in details.

                                    • 15. Re: Unterminated String Literal..?
                                      try67 MVP & Adobe Community Professional

                                      I copied and pasted the code you provided and it ran fine.

                                      • 16. Re: Unterminated String Literal..?
                                        Test Screen Name Most Valuable Participant

                                        Oh, in the console select all of the code before pressing Enter or it just runs the current line.

                                        • 17. Re: Unterminated String Literal..?
                                          alanomaly Level 1

                                          Thanks for the tip! This console is very different to the browser consoles I'm used to.

                                           

                                          Thanks for the help everyone, here's some more testing results after updating from Acrobat 10.1.5 to 10.1.7:

                                           

                                          • In the Javascript console, the code executes fine as the others report
                                          • In the Actions panel javascript tool:
                                            • With the editor set to the built-in editor, the errors happen exactly the same as before
                                            • With the editor set to an external text editor, saving changes crashes Acrobat exactly the same as before.

                                           

                                          So it seems like this is a specific bug with the execution of Javascript in the Actions tool.

                                           

                                          In my case, I can just use the console instead (thanks for the tips). If someone else runs into this bug, can't sacrifice any random lines of code to the Acrobat spirits, and needs to set it up as an action rather than just using the console, seems like they need to get Adobe to do some bug fixing.

                                          • 18. Re: Unterminated String Literal..?
                                            try67 MVP & Adobe Community Professional

                                            I highly doubt that's the problem. More likely you have some other bug in

                                            your code somewhere.

                                            What application did you use to write the code?

                                            • 19. Re: Unterminated String Literal..?
                                              alanomaly Level 1

                                              It's the exact same code, copied and pasted, with two different results depending on whether its pasted into the actions tool or the javascript console.

                                               

                                              If it makes any difference, I wrote the original code in the text editor Sublime Text, modified from code taken from another Adobe forum thread, but it makes no difference whether I copy and paste the code from there, or from the browser using the code posted above, or if I modify it in the built in Acrobat actions tool popup code window itself.

                                               

                                              The same code has one result in the console and another result in the Actions tool, and this is consistent regardless of whether the code was modified in the external text editor, in the built-in text editor, or copied from a web browser.

                                               

                                              Does it work for you if you run it as an action, rather than from the console?

                                              • 20. Re: Unterminated String Literal..?
                                                Test Screen Name Most Valuable Participant

                                                In an action it worked to the extent that I was prompted for a data source, chose a random file, and was prompted to save; then cancelled.

                                                • 21. Re: Unterminated String Literal..?
                                                  try67 MVP & Adobe Community Professional

                                                  That means there were no syntax errors, or it wouldn't have ran at all.

                                                   

                                                   

                                                  On Tue, Jul 30, 2013 at 3:09 PM, Test Screen Name

                                                  • 22. Re: Unterminated String Literal..?
                                                    alanomaly Level 1

                                                    Well, I've finally found something that seems to work reliably:

                                                     

                                                    • Copy and paste the problem code into the Acrobat javascript console.
                                                    • Make the Javascript console parse then return that code to you, by preceding the whole code with var wrapper = function() { and following it with } wrapper;
                                                    • Copy and paste the code that the console returns to you. It'll be identical but with differently arranged line breaks and whitespace, and it should now work in the Actions panel.

                                                     

                                                    My best theory is, that there's something to do with character encodings that the Actions panel is sensitive to, which the console isn't sensitive to. What this is, I don't know, but since it relates to the numbers of parsed lines added and deleted (and not which lines are deleted, and not pure whitespace lines that won't be parsed), it might be related to line break encoding. The specific error messages thrown, and the way they are always near the end of the document, are consistent with a theory that the Acrobat syntax checking tool may be incorrectly counting the length of the document and cutting it short - which could be mid-string, mid-block or mid-parenthesis.

                                                     

                                                    The Javascript console, which isn't affected by this issue, parses the code (removing whitespace etc), stores it in memory, then re-prints it using only whatever characters or encodings Acrobat is used to, which then don't cause this problem in the Actions window.

                                                     

                                                    I've tried pasting this 'clean' code back into Sublime Text and adding extra lines and extra code to it there, and that seems to make no difference - it doesn't break it - so whatever the source of the problem is, it doesn't seem to be related to any specific text editor.

                                                     

                                                    That's my best theory anyway. The important thing is, this works...

                                                     

                                                    ----------

                                                     

                                                    Edit: shortly after posting this, the issue came back again after I added a few app.alert(); lines, and the above wasn't enough to fix it. What did fix it this time (other than deleting the perfectly fine new alert lines), was doing the above then copying and pasting the code into a text editor and removing the whitespace tab indent that the console had added to all lines.

                                                     

                                                    Edit2: No, not even this always works. On long scripts it pushes the imaginary error to a random line number closer to the end of the file, but the error is still there. It seems to like to have errors around line 68. No idea why.

                                                    • 23. Re: Unterminated String Literal..?
                                                      TavC Level 1

                                                      I've had this problem when pasting in code that I've written (even when I had written it in plain text mode).

                                                      Have a close look at the quote marks around the string literal on the problem line.

                                                      The marks can be different - ie. a straight quote mark before the string, and curly (typographer's) afterwards. The curly ones seem to go unrecognised. The quote marks don't make a pair, so the string is unterminated.

                                                      In the code box, retype the quote marks over the top of the pasted in quote marks, and this might fix it for you. It has done for me.

                                                      Tav