4 Replies Latest reply on Nov 16, 2011 11:05 PM by designmon79

    save text file on desktop

    designmon79

      Hi Everyone,

       

      See my below code. It saves text file within root folder. But I want to save my report text file on desktop within a folder called report.

       

      Secondly when I runs this script twice it adds comments twice in the report text file and does not replace previous report text file.

       

      Can anyone please correct this JS script.

       

       

       

      function write(text){

      var myDocName=app.activeDocument.name;

      var myFilePath1=+ "/" + "Report" + ".txt";

      var myTextFile = new File(myFilePath1);

      if ( myTextFile.exists )

      {

      myTextFile.open("e");

      myTextFile.seek(0, 2);

      }

      else {

      myTextFile.open("w");

      }

      myTextFile.write(text+"\r");

      flag=true;

      myTextFile.close();

      //myTextFile.execute();

      if (flag==false ){

      alert("No error")

      }

      }

       

      Thanks

        • 1. Re: save text file on desktop
          [Jongware] Most Valuable Participant

          It saves text file within root folder. But I want to save my report text file on desktop within a folder called report.

           

          Yeah. These lines do that: (although there is an error in the 2nd line)

           

          var myDocName=app.activeDocument.name;

          var myFilePath1=+ "/" + "Report" + ".txt";

          var myTextFile = new File(myFilePath1);

           

          Use

           

          var myFilePath1 = new File(Folder.desktop+"/Report.txt");

           

          instead. I don't get the reference to 'myDocName', it seems unused so I'm ignoring it (as is Javascript).

           

          Secondly when I runs this script twice it adds comments twice in the report text file and does not replace previous report text file.

           

          Well yeah, it does exactly what it's supposed to do:

           

          if ( myTextFile.exists )
          {
          myTextFile.open("e");
          myTextFile.seek(0, 2);
          }
          else {
          myTextFile.open("w");
          }
          

           

          You can read top-down what happens when the file already exists. Remove this entire if .. else section and just use plain

           

          myTextFile.open("w");
          

           

          -- although I would advise to test if the opening worked, using

           

          if (!myTextFile.open("w"))
          {
             alert ("Uh-oh it dint work");
             exit(0);
          }
          
          1 person found this helpful
          • 2. Re: save text file on desktop
            designmon79 Level 1

            Hi Jong,

             

            Thanks for your support.

             

            Save file on desktop answer is right.

             

            Regarding second question It is still not what I want. I required a JS code which replace my desktop report text file (if exit) and write fresh comment text in the file. When I run my script it adds duplicate comments at the end of the exsiting file. I mean it does not delete old comments in the report text file.  It works fine if file does not exit on the given path.

             

            I tried given suggestion, It replaces report file but also delete text from the file. Actually, I have created 3 functions in JS which check InDesign file and write comments accordingly in the text file.

             

            Is there any other way to write a text file.

             

             

            Thanks,

            Mon

            • 3. Re: save text file on desktop
              [Jongware] Most Valuable Participant

              You have contradicting wishes.

              Compare this sentence

               

              designmon79 wrote:

              I required a JS code which replace my desktop report text file (if exit) and write fresh comment text in the file.

               

              with this

               

              It replaces report file but also delete text from the file.

               

              It seems obvious to me that if you replace the previous file, the old contents will be deleted.

              • 4. Re: save text file on desktop
                designmon79 Level 1

                I am really sorry. I couldn't clarify my question.

                 

                Anway, you are Genius. I mean it.