8 Replies Latest reply on Sep 24, 2012 2:29 PM by jeanne_zzzzzzz Branched to a new discussion.

    addWatermarkFromText   syntax error

    jddx1 Level 1

      I am trying to extract pages and add headers  with a script.

      I run the script by hitting cTRL+ENTER at each semicolon (in the acrobat_x_pro javascript window).

      The extraction and saving of files works just fine.

       

      But the header (this.addWatermarkFromText) will not work.

       

      I am getting a SyntaxError with this Source code  is:

       

      SyntaxError: syntax error

      1:Console:Exec

      undefined

       

      //================================================

       

      this.extractPages(0,0);

      this.saveAs("/btc-nas1/jyoung/_24X_fall_2012/pdf/INST240_sec1/INST240_sec1_fm.pdf");

      this.closeDoc(true);

      this.extractPages(23,48);

      var strFilename = "INST240_sec1_day1";

      this.addWatermarkFromText({

      cText: strFilename,

      nTextAlign: app.constants.align.right,

      nHorizAlign: app.constants.align.right,

      nVertAlign: app.constants.align.top,

      nHorizValue: -72, nVertValue: -72

      });

      this.saveAs("/btc-nas1/jyoung/_24X_fall_2012/pdf/INST240_sec1/INST240_sec1_day1.pdf");

      this.saveAs("/btc-nas1/jyoung/_24X_fall_2012/pdf/INST240_sec1/INST240_sec1_day1_quiz.pdf") ;

      this.closeDoc(true);

       

      //================================================

       

      I get a similar error when I try to insert a page:

       

      this.insertPages ({

      nPage: -1,

      cPath: "/btc-nas1/jyoung/_24X_fall_2012/pdf/INST240_sec1/INST240_sec1_fm.pdf",

      nStart: 0

      });

       

      SyntaxError: syntax error

      1:Console:Exec

      undefined

       

       

      Help  would be greatly appreciated.

        • 1. Re: addWatermarkFromText   syntax error
          try67 MVP & Adobe Community Professional

          There are various issues with your script that you're not seeing because of the way you execute it. You should select the entire code and then execute it to get a closer result to how it should work as a whole. Once you'll do that it will be easier to pick out the mistakes and fix them.

          • 2. Re: addWatermarkFromText   syntax error
            Test Screen Name Most Valuable Participant

            What is "this" in the context of the JavaScript debugger? Is it usefully the front document, or is it something else?

            • 3. Re: addWatermarkFromText   syntax error
              try67 MVP & Adobe Community Professional

              Yes, it's the currently active document (if one exists), or a generic Root object otherwise.

              • 4. Re: addWatermarkFromText   syntax error
                Test Screen Name Most Valuable Participant

                That's useful, and I can now see why the code partially worked.

                 

                I assume that if you run two commands, the first of which changes the active document, that in the second command "this" will be the newly active document. But is it so dynamic that this works even within a single command, or is "this" bound at the start of the command?

                 

                Is the behaviour written down anywhere by Adobe? The JS API seems silent on the matter, despite a useful discussion of "this".

                • 5. Re: addWatermarkFromText   syntax error
                  try67 MVP & Adobe Community Professional

                  Exactly, that's one of the problems with the code.

                  Actually, Acrobat isn't always very consistent on this issue and it's best

                  not to rely on the "this" object when working with multiple documents. In

                  such cases I prefer to maintain a variable pointing to each document that I

                  want to work with, since sometimes the "this" object changes during

                  execution, and at other times it doesn't (and no, this is not very well

                  documented).

                   

                  For example, if you extract some pages of a document to a new file, the new

                  file is opened and becomes active, but is not pointed to by "this". So

                  running this code (as a whole script!):

                  this.extractPages();

                  app.alert(this.path);

                   

                  Will show the path of the original file, not the newly extracted document.

                  But if you run the same code one line at a time, the alert will show the

                  path of the new temporary file created by the extractPages command...

                  • 6. Re: addWatermarkFromText   syntax error
                    Test Screen Name Most Valuable Participant

                    Interesting, thanks. I suspect part of the problem is that beginners find many examples using "this" and string them together without ever really examining what "this" means.

                     

                    I think I would use a simple rule:

                    1. Don't use "this" if there is any alternative.

                    2. Never, ever use "this" in a subroutine

                    3. If you find you have to use "this", assign it to a variable at the very start so it is used only once.

                     

                    I would be very reluctant to depend on "this" in a JavaScript console altogether, but if I were prepared to use it I could end up with

                    var fullDoc = this ;

                    var newDoc = fullDoc.extractPages ( nStart : 0 ) ;

                    ...

                    • 7. Re: addWatermarkFromText   syntax error
                      try67 MVP & Adobe Community Professional

                      I agree with your recommendations, although for most simple scripts there

                      shouldn't be a problem using it, and also in the console it's pretty safe,

                      unless you work with multiple documents.

                       

                      Your second rule, regarding sub-routines, is a good one to remember. Also,

                      I would refrain from using "this" in folder-level scripts, especially those

                      that are executed when the app opens.

                       

                      Another thing to avoid is dropping "this" at all, as it can be used

                      implicitly, for example:

                      extractPages(0);

                      • 8. Re: addWatermarkFromText   syntax error
                        jeanne_zzzzzzz Level 1

                        somehow, I got 2 discussions going, in error.

                         

                        See http://forums.adobe.com/thread/1070222?tstart=0