6 Replies Latest reply on Aug 31, 2009 7:27 AM by wreyfmonkey

    [JS] Get Timestamp, then Put Result in a Field

    wreyfmonkey

      Hi all,

       

      I am pretty savvy with Applescript but I am a total newbie to JavaScript.  I am trying to make a button to place in an interactive PDF that, when pressed, will get a timestamp, then place that timestamp in a locked field.  So far, I have the button and field made (easy enough), and I even have the following code to get a Unix standard time timestamp which I have added to the button as an action (I'm assuming that is the best way to do it). 

       

      Math.floor(new Date().getTime()/1000)

       

      The problem that I am having is that I don't know how to place the result of this script into a text field.  With Applescript, the result would have a variable name and the field would have a variable name and I could easily run the script and "set contents of theField to theResult" or something similar.  But I have no idea how to do this seemingly simple task with JavaScript.  I've scoured the Internet and these forums and can't find anything helpful to me as a beginner, so any help would be much appreciated.  Thanks so much!!!

        • 1. Re: [JS] Get Timestamp, then Put Result in a Field
          gkaiseril MVP & Adobe Community Professional

          You should read about Mozilla JavaScript and the Acrobat JavaScript object. Material is freely available at the Mozilla developer's site and Adobe's Developer site.

           

          The JavaScript date object is not a number it is an object that can provide a number of various date time values through the use a different methods and these methods are documentated as noted above.

           

          To produce a resutl the you maybe able to use one could use the following code for the "Mouse Up" JavaScirpt action to fill a field named "TimeStamp":

           

          // get the current date object
          var oNow = new Date();
          // get the milliseconds from the Epoch date
          var fTime = oNow.getTime();
          // trim to seconds
          var fSeconds = fTime / 1000;
          // turncate to seconds
          var fTimeStamp = Math.floor(fSeconds);
          // populat the 'TimeStamp' field's value
          this.getField('TimeStamp').value = fTimeStamp;
          // some debugging code
          console.show();
          console.clear();
          console.println('date object: ' + oNow);
          console.println('valueOf() date object: ' + oNow.valueOf());
          console.println('time in milliseconds: ' + fTime);
          console.println('time in seconds: ' + fSeconds);
          console.println('turncated seconds: ' + fTimeStamp);
          console.println('rounded seconds: ' + Math.round(fSeconds) );

          1 person found this helpful
          • 2. Re: [JS] Get Timestamp, then Put Result in a Field
            wreyfmonkey Level 1

            GKaiseril,

             

            Thanks for the quick reply!  That was exactly what I needed, though I did remove the debugging code to keep the JavaScript debugging console from showing when the script executes.  And I did bookmark those Mozilla resources you mentioned.  I am incredibly new to object oriented programming, so I am very grateful for your help.  If you wouldn't mind, I'm now wondering how I might add a lettered prefix to the timestamp.

             

            For example, if I have a PDF that contains a drop down menu and if "Atlanta" is selected, I would need the PDF timestamp to have something like "ATL1254879634".  How would I set up a variable to tell the script that "Atlanta" should equal "ATL" when the prefix is thrown into the TimeStamp field?  ...And how would I combine the prefix and the timeStamp?

             

            Just so you know, I really appreciate your time and help.  I'm actually reading through and trying to understand what you write, rather than just mindlessly copying/pasting your code.  So thanks again for taking the time to give me a good head start in JavaScript and thanks in advance for the help!

            • 3. Re: [JS] Get Timestamp, then Put Result in a Field
              gkaiseril MVP & Adobe Community Professional

              You need to create the Combo Box with the display text and the export value as needed, you can then use the string concatenation operator, '+', to join the values together.

              • 4. Re: [JS] Get Timestamp, then Put Result in a Field
                wreyfmonkey Level 1

                Thanks for the reply again, George.  I kept playing with it yesterday and I figured it out.  I already had a combo box in the PDF with a list of items.  I just needed to somehow tell the script that "if A is selected in the combo box, then put B + timestamp into this other box."  Here's what I did (excerpted from the fuller script and edited for clarity):

                 

                // get the current date object

                var oNow = new Date();

                // get the milliseconds from the Epoch date

                var fTime = oNow.getTime();

                // trim to seconds

                var fSeconds = fTime / 1000;

                // truncate to seconds

                var fTimeStamp = Math.floor(fSeconds)

                // set the City field to a variable

                var fCityField = this.getField('City');

                // Get value of the City combo box as variable

                var citySelection = getField("City").value;

                // assign each city an abbreviated name code.

                //If the combo box selection is Atlanta, then the name code should be ATL

                if (citySelection == "Atlanta") var nameCode = "ATL";

                // populate the 'TimeStamp' field's value

                this.getField('TimeStamp').value = nameCode + fTimeStamp;

                 

                RESULT: ATL1251466184...which is exactly what I was looking to do.  I needed a custom city code based on the user's combo box selection AND the timestamp.  Perfect!

                 

                One last question: How would I use JavaScript to tell the PDF to "Save As" with the save name being this timestamp string?  I can't seem to change the default text in the "Save As" dialog with the script.  I had something close but I kept getting a permissions error.  Thanks! 

                • 5. Re: [JS] Get Timestamp, then Put Result in a Field
                  gkaiseril MVP & Adobe Community Professional

                  There are special security restrictions placed on this acton by Adobe.

                   

                  You should down load the Acrobat JavaScript documnentation. You will need to write a special application folder level trusted function. All your users will need to install this custom JavaScript file.

                  1 person found this helpful
                  • 6. Re: [JS] Get Timestamp, then Put Result in a Field
                    wreyfmonkey Level 1

                    Hey George,

                     

                    Thanks for pointing me to that API reference guide.  I downloaded it and I know it'll come in handy.

                     

                    Thanks for explaining why I can't script a default save name for my doc, too.  Unfortunately, writing a script like that is not possible for me since this PDF is a form for clients, rather than a set group of users.  I would never be able to install it on their machines because it would be impossible to know who they are until they sent the form.  That being the case, is there any other way that you (or anyone) know of to save a PDF with a pre-chosen name using JavaScript?  It seems strange that Adobe would block that functionality completely.