8 Replies Latest reply on May 1, 2013 4:59 PM by gkaiseril

    Saving Javascript using Action Wizard

    Alex19822

      Hi

       

      Does anyone know how to add a Javascript Action on the first page of a document using Action Wizard? What I normally do is open the first page properties, choose action and then save the javascript so it runs when the document is opened but I normally have around 30 to 50 files at a time. I've tried looking at Action Wizard which would save me a great amount of time but can't see any settings to save the javascript to the first page.

       

      Any help would be much appreaciated.

       

      Thank you

       

      Alex

        • 1. Re: Saving Javascript using Action Wizard
          Bernd Alheit Adobe Community Professional & MVP

          In an action you can execute Javascript. There you can use the setPageAction.

          • 2. Re: Saving Javascript using Action Wizard
            Alex19822 Level 1

            Apologies to sound stupid but I'm quite new to adobe acrobat. When I click on the options it opens a window to exectue a javascript but nothing else.

            • 3. Re: Saving Javascript using Action Wizard
              Alex19822 Level 1

              Sorry, do you mean in the code?

               

              I'm using this script;

               

              // Get Current Date

              var curDate = new Date();

              // Get Final date

              var finalDate = new Date("05/14/2013");

              // Compare Dates

              if(finalDate.getTime() < curDate.getTime())

              { // Past closing Date, expire document

              app.alert("These listings have now expired.  Please contact Head Office");

              this.closeDoc(true);

              }

               

              Do you know where to put it?

               

              Thanks

               

              Alex

              • 4. Re: Saving Javascript using Action Wizard
                gkaiseril MVP & Adobe Community Professional

                As stated you need to use JavaScript in the Action manager to add the script action and edit the JavaScript to run. There are many ways and locations use to add a script to a PDF. For adding a script by JavaScript to a page you use the sePageAction method. You need to supply the zero based page number for the page you want to add the script to, the trigger or what causes the event to run, and the text of the script to insert. For page 1 the zero based page number is 0, for the trigger action the value is "Open". The tricky part is converting your script to a variable text string since you have quotation marks within the script you need to work out how best to avoid them or not have them terminate your string.

                 

                var MyScirpt = "// Get Current Date;\nvar curDate = new Date();\n// Get Final date;\n" +

                "var finalDate = new Date(2013, 04, 14);\n" +

                "// Compare Dates;\nif(finalDate.getTime() < curDate.getTime()) {\n" +

                "// Past closing Date, expire document;\n" +

                "app.alert('These listings have now expired.  Please contact Head Office');\n" +

                "this.closeDoc(true);\n}";

                this.setPageAction({nPage: 0, cTrigger: "Open", cScript:  MyScript});

                • 5. Re: Saving Javascript using Action Wizard
                  Alex19822 Level 1

                  Thanks for your help but this didn't work.

                  • 6. Re: Saving Javascript using Action Wizard
                    gkaiseril MVP & Adobe Community Professional

                    were there any errors?

                     

                    Can you get the script to work in the JavaScript console?

                     

                    Check the name for the variable for the script to insert.

                     

                    I changed the way the "new Date()" was used to avoid having to use a quoted date string value.

                    • 7. Re: Saving Javascript using Action Wizard
                      Test Screen Name Most Valuable Participant

                      I assume you corrected the spelling mistake in the first line as you retyped it...?

                      • 8. Re: Saving Javascript using Action Wizard
                        gkaiseril MVP & Adobe Community Professional

                        Too much time has passed, I cannot edit it.

                         

                        One can only expect so much for free.

                         

                        var MyScript = "// Get Current Date;" +

                        "\nvar curDate = new Date();\n" +

                        "// Get Final date - yyyy, mm, dd - month is zero based;\n" +

                        "var finalDate = new Date(2013, 04, 14);\n" +

                        "// Compare Dates;\n" +

                        "if(finalDate.getTime() < curDate.getTime()) {\n" +

                        "// Past closing Date, expire document;\n" +

                        "app.alert('These listings have now expired.  Please contact Head Office');\n" +

                        "this.closeDoc(true);\n}";

                         

                        this.setPageAction({nPage: 0, cTrigger: "Open", cScript:  MyScript});

                         

                        You should first test this using the JavaScript console and get it debugged, Then you could add it to a batch process. If you need to enter the date as a string, I'll let you figure that out. Of course editing scripts can easily produce errors especially when the script is generating a new script.