10 Replies Latest reply on May 6, 2012 8:26 AM by Mr Clint Eastwood

    Create files with javascript?

    Mr Clint Eastwood

      Hello,

       

      I would like make a config file for my script that i would like to put in the %appdata%\adobe folder.

       

      1. How can i reah the value of %appdata% inside javascript?
      2. How do i create and read files?

       

      I am currently reading the Extendscript javascript toolsguide but i cant figure out how to create a file:

       

      Should not this code create a file in C:\ directory?

       

      Fille = new File('C:\test.txt');

      Fille.write("Testwriting!");

      Fille.close();

       

      / asdfasdfasdf

        • 1. Re: Create files with javascript?
          John Hawkinson Level 5

          How can i reah the value of %appdata% inside javascript?

          APPDATA is an environment variable, so $.getenv("APPDATA").

           

          Should not this code create a file in C:\ directory?

          new FIle() merely creates a reference to a [potential] file. You must open the file for writing first.

          But it's quite bad style to start variable names with an uppercase letter -- uppercase first letters in Javascript should be reserved for Constructors, like new File(). So you want something like:

           

          fille = new File('C:\\test.txt'); 
          fille.open();
          fille.write("Testwriting!");
          fille.close();
          

           

          You must also double the backslash in a quoted string.

          1 person found this helpful
          • 2. Re: Create files with javascript?
            Mr Clint Eastwood Level 1

            Great!

             

            the $.getenv("APPDATA") worked like charm but unfortunately not the file code.

             

            I run exactly the code that you wrote and it does not working. I have changed the path if i for some reason did not have any right in the C: root no

             

             

            Any Ideeas?

             

            EDIT:

             

            With some googleing i found some code that works:

             

            fille.open("w");

             

            WriteMode for the win!

             

            Thanks for the help!!

            • 3. Re: Create files with javascript?
              Muppet Mark Level 5

              You should also look at the class properties of the folder object…

               

              Folder.appData

               

              Should give you the same path? There are several others of use there so take a look… Yes you have 4 options of file open( ) also in guide…

              • 4. Re: Create files with javascript?
                Mr Clint Eastwood Level 1

                Great info! Thanks!

                 

                Although i think i solved it a little bit wierd, i dont know if i am using it wrong or if it is a bug but i cant use the open("e");

                 

                Example:

                This should work but i t does not, it reads the data and change the DefaultSavePath correctly, but it cant save it.

                 


                var appdata = $.getenv("APPDATA")

                var xmlFile = new File(appdata + "\\adobe\\Settings.xml");

                xmlFile.open("e");

                var xml = new XML (xmlFile.read());

                xml.DefaultSavePath = "Testing AGAIN";

                xmlFile.close();

                 

                I solved it with:

                 


                var appdata = $.getenv("APPDATA")

                var xmlFile = new File(appdata + "\\adobe\\Settings.xml");

                xmlFile.open("r");

                var xml = new XML (xmlFile.read());

                xml.DefaultSavePath = "Testing AGAIN";

                xmlFile.close();

                xmlFile.open("w");

                xmlFile.write(xml);

                xmlFile.close();

                 

                 

                Do you see what i am doing wrong?

                 

                EDIT: I have no idea why the code for some reason ended up in some kind of tables, but whatever!

                • 5. Re: Create files with javascript?
                  Muppet Mark Level 5

                  Which option did you pick… to paste the code in? You should click 'Use advanced editor' top right corner and go to the blue chevrons…

                   

                  Screen shot 2012-05-06 at 11.29.03.png

                  I had one go all weird on me yesterday with bolding and colours…

                   

                   

                  var xmlFile = new File( Folder.appData + '/Adobe/Settings.xml' );
                  
                  xmlFile.open( 'w' );
                  
                  var xml = new XML ( xmlFile.read() );
                  
                  //xml.DefaultSavePath = 'Testing AGAIN'; // Where did you find this default thing???
                  
                  xml.addNamespace( 'http://www.adobe.com/test' ); // I don't know XML so a guess…
                  
                  xmlFile.write( xml );
                  
                  xmlFile.close();
                  
                  xmlFile.execute();
                  
                  • 6. Re: Create files with javascript?
                    John Hawkinson Level 5

                    Example:

                    This should work but i t does not, it reads the data and change the DefaultSavePath correctly, but it cant save it.

                    ...
                    Do you see what i am doing wrong?

                    EDIT: I have no idea why the code for some reason ended up in some kind of tables, but whatever!

                    Err, I don't understand your question.

                    It sounds like you are asking why your first example does not save your change to DefaultSavePath.

                    The answer is that you are not even attempting to save the change.

                     

                    You read the contents of Settings.xml into a variable called xml and then you modify that variable. There is no persistent connection between the variable and that variable and the original file. You should have no expectation that changes to the variable will change the file. They won't. It's just a data structure in memory. Did you have some reason to think otherwise?

                     

                    With respect to tables, the "new" version of the forum software does this when the first line of pasted code starts with a blank space. As MuppetMark said, use the Java syntax highlighting. But also, just make sure the first line that you copy does not start with a space.

                    • 7. Re: Create files with javascript?
                      Mr Clint Eastwood Level 1

                      Aah, nice, the Advanced Editor

                       

                      So it is hiding over there!

                       

                      The "xml.DefaultSavePath" comes from my xml file wich looks like this:

                       

                       

                      <ScriptRoot>
                        <DefaultSavePath>Testing</DefaultSavePath>
                      </ScriptRoot>
                      

                       

                      Example:

                       

                       

                      var xml = new XML ( 
                      <ScriptRoot>
                        <DefaultSavePath>Testing</DefaultSavePath>
                        <DefaultSavePath>C:\program files\adobe</DefaultSavePath>
                        <DefaultSavePath>B:\adobe</DefaultSavePath>
                      </ScriptRoot>
                      );
                      
                      
                      alert(xml.DefaultSavePath[0]) // Returns: Testing
                      alert(xml.DefaultSavePath[1]) // Returns: C:\program files\adobe
                      alert(xml.DefaultSavePath[2]) // Returns: B:\adobe
                      
                      
                      

                       

                      So what i am trying to do is to change that value and write that to the file but i cant do it with the e option.

                      • 8. Re: Create files with javascript?
                        Mr Clint Eastwood Level 1

                        OMG sorry, forgot the .write method when i posted it!

                         

                        For clearing up!

                         

                        This does not work!

                         

                        var appdata = $.getenv("APPDATA")
                        var xmlFile = new File(appdata + "\\adobe\\ExportAsPNG_Settings.xml"); 
                        xmlFile.open("e");
                        var xml = new XML (xmlFile.read());
                        xml.DefaultSavePath = "Testing AGAIN";
                        xmlFile.write(xml);
                        xmlFile.close();
                        
                        

                         

                        This Works:

                         

                        var appdata = $.getenv("APPDATA")
                        var xmlFile = new File(appdata + "\\adobe\\ExportAsPNG_Settings.xml"); 
                        xmlFile.open("r");
                        var xml = new XML (xmlFile.read());
                        xml.DefaultSavePath = "Testing AGAIN";
                        xmlFile.close();
                        xmlFile.open("w");
                        xmlFile.write(xml);
                        xmlFile.close();
                        
                        

                         

                        This is what i have to do:

                        1. Open the file in Read mode
                        2. Read the file into a variable
                        3. Modify the variable
                        4. Close the file
                        5. Open the file in write mode
                        6. Write the variable to the file
                        7. Close the file.
                        • 9. Re: Create files with javascript?
                          John Hawkinson Level 5

                          Opening a file read/write merely means that it is possible to both read and write to the file.

                          I'm not sure that it's such a good idea to try to do both at once.

                          How does your code using xmlFile.write() fail? You don't say!

                           

                          I would expect that the contents of Settings.xml would be duplicated.

                           

                          When you read and when you write to a file, it advances the file pointer to which subsequent reads and writes are relative.

                          In your first example, you call .read(), which reads the file entire and advances the file pointer to the end. Then you call .write(), which should write the string representation of your xml varible to the file relative to the file pointer. So your file should have two copies of the xml: the original unmodified, followed by your modified version.

                           

                          Is that what happens? (Again, all you said was "does not work"; that is pretty much never sufficient! There is always more information you can provide that will help people help you.)

                           

                          If you really want to do it this way, you can call xmlFile.seek(0); before you write.

                          I'm not sure, though, whether the .close() method will truncate the file. If you shorten the xml data, will the original excess still be there?

                          You could, I suppose, call xmlFile.tell(); before you close, and then set xmlFile.length to that value afterwards.

                           

                          I suppose you might still have this problem if you open the file for writing. I'm not sure. Something to test.

                          • 10. Re: Create files with javascript?
                            Mr Clint Eastwood Level 1

                            Sorry for the lack of information!!

                             

                            My first example that does not work just does not work

                            I does not get any error message and the modified date for the file does not change. Nothing happens

                             

                            If i put an alert in the code:

                             

                             

                            var appdata = $.getenv("APPDATA")
                            var xmlFile = new File(appdata + "\\adobe\\ExportAsPNG_Settings.xml"); 
                            xmlFile.open("e");
                            var xml = new XML (xmlFile.read());
                            xml.DefaultSavePath = "Testing AGAIN";
                            alert(xml);
                            xmlFile.write(xml);
                            xmlFile.close();
                            

                             

                            It will alert the xml variable with correct changes.

                             

                            In my other example that works the whole file will be replaced with my xml variable and that is how i want it to be

                             

                            I am okey with using both W and R mode, I just thought it was weird that the E option did not work.