3 Replies Latest reply on Sep 28, 2015 10:10 AM by Kasyan Servetsky

    Is it possible to receive an error message from “glue code”?

    Kasyan Servetsky Level 5

      Dear all,

       

      I wrote a script that applies styles using xPath using two modes:

      • xml-rules
      • evaluate  xPath expression

       

      For example, I want to get the first book element that is the child of the bookstore element:

      /bookstore/book[last()]

      This works in the “evaluate xPath expression” mode.

      27-09-2015 13-55-45.jpg

       

      If no elements have been found, the script gives a warning.

      28-09-2015 13-50-14.jpg

      But in the xml-rules mode it doesn’t work because, as far as I understand, last() isn’t supported  in xml-rules so it silently throws an error. As a result nothing happens (no warning appears) when I click the “Find” button and no matches are found.

      28-09-2015 13-57-12.jpg

       

      Regards,

      Kasyan

        • 1. Re: Is it possible to receive an error message from “glue code”?
          Loic.Aigon Adobe Community Professional

          Hi Kasyan,

           

          I just dropped an eye on glue code and any catch instruction throws a cascading error till the initial code. So the error just be notified at the very first level. However, in a scriptUI environment, such an error may remains silent. My guess is that there is some bug in the ScriptUI layer that doesn't deal with errors at some point (that's my feeling, not a statement).

           

          Have you tried a global try catch on  your main call ? Try to put some log in it and see what happens. Once again, the error should bubble to the main call according to the glue code impelmentation.

           

          Also, you can try to run your code without passing through the UI.

           

          FWIW,

           

          Loic

          • 2. Re: Is it possible to receive an error message from “glue code”?
            Loic.Aigon Adobe Community Professional

            Indeed, it looks like XMLRules XPath implementation doesn't support XPath functions. Nor Position() nor Count() would work.

             

            You may need to output the position computation like :

             

            
            
            var getCount = function  (doc, xpath ) {
              var xf = File ( Folder.temp+"/temp.xml" );
              var x;
              doc.exportFile ( ExportFormat.XML, xf );
              xf.open('r');
              x = XML ( xf.read() );
              xf.close();
              xf.execute();
            
              return x.xpath ( "count("+xpath+")" );
            }
            var t = new test;
            var count = getCount ( app.activeDocument, "./bookstore/book" )
            t.xpath="/bookstore/book["+count+"]/title";
            var myRuleSet = [t];
            

             

             

            HTH,

             

            Loic

            • 3. Re: Is it possible to receive an error message from “glue code”?
              Kasyan Servetsky Level 5

              Hi Loic,

               

              Thank you for your quick reply.

              Yes, using try-catch block solved the problem.

              if (set.useXmlRules) {
                  var ruleSet = [new ProcessXpath];
                  with (doc) {
                      var elements = xmlElements;
                      try {
                          __processRuleSet(elements[0], ruleSet);
                      }
                      catch(err) {
                          $.writeln(err.message + ", line: " + err.line);
                      }
                  }
              }
              
              

              This returns the error from glue code.

              28-09-2015 20-07-22.jpg

              Regards,
              Kasyan