Error in runEval code: unexpected token: identifier

I’m getting the above error in the following code:

{
  "Command": "storeEval",
  "Target": "ctr=\"${QUERY}\"; var fst = ctr.charAt(0); var snd = ctr.charAt(1); if(snd == 'z'){   ctr = String.fromCharCode(fst.charCodeAt() + 1) + 'a'; } else {   ctr = fst + String.fromCharCode(snd.charCodeAt() + 1); }; ctr;",
  "Value": "QUERY"
},

All works fine with my test macro (see below), so the issue is caused by the value of ${QUERY} - maybe it contains some un-escaped line breaks?

Kantu does a simple string replacement for the variables, so line breaks will break the script. There are two solutions:

  1. Clean the string. You can see an example of this in the DemoPDFTest_with_OCR macro that ships with kantu.

  2. Work with ctr=storedVars['QUERY'] - this operates directly with the variable

Clean string:

{
  "Command": "storeEval",
  "Target": "a = storedVars['q']; a = a.replace(/( |\\n|\\r)/gm, \"\"); a",
  "Value": "q"
},

Test macro (works ok, because the variable only contains “yyy”):

{
  "Name": "1",
  "CreationDate": "2019-3-4",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://status.ocr.space/",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "yyy",
      "Value": "QUERY"
    },
    {
      "Command": "storeEval",
      "Target": "ctr=\"${QUERY}\"; var fst = ctr.charAt(0); var snd = ctr.charAt(1); if(snd == 'z'){   ctr = String.fromCharCode(fst.charCodeAt() + 1) + 'a'; } else {   ctr = fst + String.fromCharCode(snd.charCodeAt() + 1); }; ctr;",
      "Value": "QUERY"
    }
  ]
}