8 Replies Latest reply on Sep 13, 2013 3:04 PM by gkaiseril

    Processing multiple PDFs using Javascript  (Acrobat XI on the Mac)

    JADarnell Level 1

      The original specs for this project called for the Javascript to process one large file (the example I was given to use had 104 pages in it).

       

      I finished the script, turned it in and was informed that management had changed their minds.  Now they want the script to process a number of small PDFs (each file one or two pages long).  So now the user must either select a single file fifty-two times (or more) or I must find a way to process multiple files using this script.

       

      I looked at the API and there seems to be no way to select more than one PDF at a time for processing.  I do not know if that is a security thing or if they are trying to drive us to use Actions.

       

      I attempted to set up an action but my script is simply too long (in excess of 2300 lines).  When I go to the Execute Javascript step, open the Settings option and copy the script into the JS editor provided, when I run the action, Acrobat tells me that the script is "too long for this editor."

       

      The reason for the long script is that I am going through the document page by page and examining the text for URLs.  Do you know how many ways there are for different writers to express URLs?  Believe me, they are numerous! (grin)

       

      I went to preferences and changed my editor to an external editor (UltraEdit for Mac--it does plain text editing).  I then opened up settings and the UE editor did come up, but when I copy/pasted the code from the original script into the temp file created by the Action Wizard using the UE Editor, and then attempted to save the script, I got the eternal beach ball.

       

      Do I have a third option for processing a number of PDF files?  Is there something that I am overlooking that will make this script work?

       

      Thanks for your time and effort.

       

      R,

      John

        • 1. Re: Processing multiple PDFs using Javascript  (Acrobat XI on the Mac)
          gkaiseril MVP & Adobe Community Professional

          The Professional version of Acrobat has the "Action" (version X on) or "Batch Processing" ( version 9 and below). On the MacIntosh you can also use Apple Script to programmatic open a PDF and call a JavaScript function to process within that PDF.

          • 2. Re: Processing multiple PDFs using Javascript  (Acrobat XI on the Mac)
            JADarnell Level 1

            GKaiseril, thank you for your suggestion.

             

            As I have used Applescript to process multiple files in the past, it was easy to snatch that code and modify it to implement your suggestion.  However I am beating my head against the wall again in that when I run the Javascript through the Applescript I get the following error (the code runs fine directly within Acrobat):

             

            SyntaxError: Illegal Character.

            ":0"

            (I had to add the quotes to keep the program from changing it into an emoticon.  Otherwise the quotes do not really show up.)

             

            My Applescript code looks like this:

             

            tell application "Adobe Acrobat Pro"

            --  fileItem is the PDF.  The expected PDF shows up on the screen when this is run.

            open fileItem

            --JSFile contains the path to the Javascript file I want to run.

            do script file JSFile

            close document 1

            end tell

             

            The file, URLAction.js is large and complex, so to simplify things for myself, I changed the file to one containing just three lines (okay, technically I guess its seven lines):

             

            doit();

             

            function doit()

            {

               app.alert("this is the first line");

               app.alert("this is the second line");

            }

             

            This much smaller script still generated the same error message.

             

            The Applescript script is a folder action script.  Using a judicious number of "display dialog" calls I have determined that the Applescript seems to be working just fine.  Its the call to the Javascript that is not working.

             

            I have tried using different line endings to see if that might have been the case (this was a problem in other projects in the past) and that did not fix the problem.

             

            I also switched from the default encoding to Ascii after reading that that has been a problem in the past.  It resolved nothing.

             

            Does anyone have additional suggestions to try?

             

            TIA!

             

            John

            • 3. Re: Processing multiple PDFs using Javascript  (Acrobat XI on the Mac)
              Alan Shutko Level 1

              I've tried the same thing, with the JS "var a = 1;".  Still fails with

               

              SyntaxError: Illegal Character.

              ":0"

               

              I confirmed with fs_usage that Acrobat IS opening the file I'm using. 

               

              I think that do script file is just broken in Acrobat Pro XI.

              • 4. Re: Processing multiple PDFs using Javascript  (Acrobat XI on the Mac)
                gkaiseril MVP & Adobe Community Professional

                You are defining your function after you have tried to use it. Since the function is not defined when it was called how can it run?

                 

                Also using small names can sometimes cause an issue with other scripts that use the same short names.

                 

                This appears more of a problem in using AppleScript to run JavaScript.

                • 5. Re: Processing multiple PDFs using Javascript  (Acrobat XI on the Mac)
                  Alan Shutko Level 1

                  My entire js literally consists of

                   

                  var a = 1;

                   

                  My applescript is

                   

                  tell application "Adobe Acrobat Pro"

                    activate

                            set myFile to "/Users/ats/bin/scripts/extract-pages.js"

                   

                    do script file myFile

                  end tell

                   

                  and I still get the error.

                   

                  Can you post a minimal test case that works with Applescript and Acrobat Pro XI?  I've tried running the script from within Acrobat as part of an action and it works, so the problem is the Applescript->Acrobat connection, not the script.

                  • 6. Re: Processing multiple PDFs using Javascript  (Acrobat XI on the Mac)
                    JADarnell Level 1

                    I resolved this issue by creating a two-step process.  The first step was to use an applet to read the files in the requisite folder, and then build the javascript with the files included in the script as literals.  To make this ann easier task, I created a template script that is opened like a text file by the applet, modified with the filenames, and then saved as a .JS file. 

                     

                    Then after running the applet I opened up Acrobat and selected a menu entry I added using the config.js file.  The menu entry executes the .JS file made from the template.

                     

                    Though the two-step process is a bit more complex than I would like, it has one overriding advantage; it works and it works today.

                     

                    Alan, I agree with you; I think the do script file command in Applescript is broken when it comes to executing javascript. Thanks for your support.

                    • 7. Re: Processing multiple PDFs using Javascript  (Acrobat XI on the Mac)
                      JADarnell Level 1

                      Mr. Kaiseril:

                       

                         My normal mode for writing Javascript is to write as one of the first executable lines the driver for the full Javascript.  In other words, all of my Javascripts look very similar to this:

                       

                      MainFunc();

                       

                      function MainFunc()

                      {

                         foo();

                         bar();

                         foobar();

                      }

                       

                      function foo()

                      {}

                       

                      function bar()

                      {}

                       

                      function foobar()

                      {}

                       

                      I have never had a problem so doing in the past.  I admit my experience base is small and II don't mean to be mean, but even one example that works using the above style would disprove your assertion, unless of course you are saying that only in Applescript run JS does the order of function definition/usage matter. 

                       

                      Additionally, the example I cited in my original post was exactly that, an example.  The real code uses much longer and much more explicit function names.

                       

                      Finally, the question has become moot, for I found a solution that works, and works today.  Please see my previous post in this thread.

                      • 8. Re: Processing multiple PDFs using Javascript  (Acrobat XI on the Mac)
                        gkaiseril MVP & Adobe Community Professional

                        That script might not work within a PDF. Many times Acrobat JavaScript needs the functions defined before being called.  It also does not support "Main Func();".

                         

                        This forum is mostly for using JavaScript within Acrobat.