6 Replies Latest reply on Nov 28, 2012 1:56 PM by Nifty Styles

    Importing text data to a text field - What am I doing wrong ?

    Nifty Styles

      I've been pulling my hair out over this for half the weekend

       

      My ultimate objective is to read a date stored in a text file and display it in a text field on my PDF page.

      However, I am currently stuck at the point of simply importing the pure text (without reformatting it to the date format I would ultimately like).

       

      The text contained in my text file is simply a list of dates (of which I wish to print the first in the list). Read about the importTextdata function I have added a header line with the text fields name - so the contents of my text file looks like this:

       

      LastSyncDate

      09/10/2012 12:17:53.54

      04/10/2012 14:23:45.32

      23/07/2012 12:46:35.51

      Never

       

       

      I understand that the importTextData command is a priveleged function so I have written a folder level script to read the text file and store the text in a variable, which is then assigned to the text field.

      I have then called the trusted function at the Document Script level of the page concerned:

       

      Folder Level script:

       

      LastSyncCalc3 = app.trustedFunction( function()

      {

      app.beginPriv();

      var LastSync3 = importTextData("/C/sync/bin/LastSyncTest.txt", 0);

      this.getField("LastSyncDate").value = Lastsync3;

      app.endPriv();

      }

      );

       

      Document Level Script:

       

      LastSyncCalc3();

       

       

      Running this gives the following errors in the console:

       

      importTextData is not defined

      4:Document-Level:SyncDate

      ReferenceError: importTextData is not defined

      4:Document-Level:SyncDate

       

       

      Thinking I had my commands misplaced, I changed the folder level script and document level script to the following:

       

      Folder Level script:

       

      LastSyncCalc3 = app.trustedFunction( function()

      {

      app.beginPriv();

      var LastSync3 = importTextData("/C/sync/bin/LastSyncTest.txt", 0);

      app.endPriv();

      }

      );

       

       

      Document Level Script:

       

      this.getField("LastSyncDate").value = Lastsync3;

       

       

      Running this gives the following errors in the console:

       

      Lastsync3 is not defined

      1:Document-Level:SyncDate

      ReferenceError: Lastsync3 is not defined

      1:Document-Level:SyncDate

       

       

      Where am I going wrong please ??

        • 1. Re: Importing text data to a text field - What am I doing wrong ?
          try67 MVP & Adobe Community Professional

          importTextData is a method belonging to a Doc object. You can use it

          implicitly in many cases, but not in the context of a folder-level script.

          You'll need to pass the current doc object as a parameter to the function,

          and then use it when calling the function (the same goes for the use of

          "this" as a reference to the document).

           

          There are other problems with your code, though. For example, you define a

          variable called "LastSync3", but then try to access one called "Lastsync3".

          JS is case-sensitive, so you have to use the exact same name.

          Also, are you aware that importTextData returns a number indicating if the

          process was successful or not, not a date?

          • 2. Re: Importing text data to a text field - What am I doing wrong ?
            Nifty Styles Level 1

            Gilad,

             

            Sorry for the long delay in resuming this project.

             

            I am really stumped and slightly baffled by the technical speak in your last post - in particular:

             

            "You'll need to pass the current doc object as a parameter to the function,

            and then use it when calling the function "

             

            Would you be so kind as to put in more simple terms what code I need to put where please ?

            I've tried various combinations but with no success !!

             

            Thank you  in advance,

            Nifty.

            • 3. Re: Importing text data to a text field - What am I doing wrong ?
              try67 MVP & Adobe Community Professional

              Instead of this:

               

              Folder Level script:

               

              LastSyncCalc3 = app.trustedFunction( function()

              {

              app.beginPriv();

              var LastSync3 = importTextData("/C/sync/bin/LastSyncTest.txt", 0);

              this.getField("LastSyncDate").value = Lastsync3;

              app.endPriv();

              }

              );

               

              Document Level Script:

               

              LastSyncCalc3();

               

              Use this:

               

              Folder Level script:

               

              LastSyncCalc3 = app.trustedFunction( function(doc)

              {

              app.beginPriv();

              var LastSync3 = doc.importTextData("/C/sync/bin/LastSyncTest.txt", 0);

              doc.getField("LastSyncDate").value = Lastsync3;

              app.endPriv();

              }

              );

               

              Document Level Script:

               

              LastSyncCalc3(this);

              • 4. Re: Importing text data to a text field - What am I doing wrong ?
                Nifty Styles Level 1

                Gilad,

                 

                Superb .. that's exactly it ... so straight forward and obvious once you'd posted it up.

                I'm feeling suitably stupid and embarrassed for not cottoning on sooner !

                 

                It's a long way to come but if you're ever in my neck of the woods there's a pint or two waiting for you.

                 

                Thank you very much,

                Nifty.

                • 5. Re: Importing text data to a text field - What am I doing wrong ?
                  try67 MVP & Adobe Community Professional

                  PS. I had forgotten to fix the variable name error in the code above. So this:

                  doc.getField("LastSyncDate").value = Lastsync3;

                   

                  Should be:

                  doc.getField("LastSyncDate").value = LastSync3;

                  1 person found this helpful
                  • 6. Re: Importing text data to a text field - What am I doing wrong ?
                    Nifty Styles Level 1

                    Thank you very much, I have it working.

                     

                    (I had actually corrected the case error already).

                     

                    Now all I need to do is get the system to read the text as a date (which is what it is) so I can format it in a more friendly fashion.

                    I've other things to worry about at present but may need some help with that a little later on !!