6 Replies Latest reply on Jul 9, 2013 3:54 AM by Muppet Mark

    Can a JSX script over-write itself?

    jsavage77 Level 1

      I'm looking for a way for a script to modify it's own code.  It this possible, or would the file be locked while the script is running?  If it's possible, would the changes take place immediately (assuming the script has yet to reach a line) or upon next launch of the script?

        • 1. Re: Can a JSX script over-write itself?
          Gustavo Del Vechio Level 3

          Do not know if there´s a way to a script over-write itself.

           

          A thing you could think about is to work with single files (reading and writting the ones). For example, a script could create a *.TXT file in any folder in the system. The script could even write (for example, the necessary preferences) in the file. So, every time user run the script, you could programm that this script could first look into *.TXT file to read it and know the preferences or what´s necessary.

           

          Does it suit your needs?

           

          Best Regards

          Gustavo.

          • 2. Re: Can a JSX script over-write itself?
            Bob Stucky Adobe Employee

            Yes, a script can overwrite itself - but don't do it. The slightest error will render the script useless.

             

            ExtendScript's File and Folder objects allow you to write files anywhere in the filesystem you have permission to do so.

             

            Use Folder.userData as the base location (typically). You can also use Folder.commonFiles.

             

            var f = new File( Folder.userData.absoluteURI + "/[myScript]/[myScript].txt );

             

            Or some such to get the file location.

             

            Then write your file. If it's a pure script, load it using $.evalFile - enclose it in a try/catch in case there's a syntax error. If it's a text file, read it and get what you need. You can also use an associative array -

             

            var a = [];

            a["prop1"] = "Fred";

            a["prop2"] = "Barney";

             

            Then, with your file:

             

            f.write( a.toSource() );

             

            That'll create a json file. To read that back into your script:

             

            var a = eval( f.read() );

             

            Make sense??

            • 3. Re: Can a JSX script over-write itself?
              Muppet Mark Level 5

              Hey Bob… are you lost this is the AI scripting forum and that's the first time in what may be 5 years I've seen "Staff" post…

               

              Now that I need to eat my hat… Im going to need a pint to help it down…

              • 4. Re: Can a JSX script over-write itself?
                Bob Stucky Adobe Employee

                Not lost, still watch all the scripting forums, and I even make a post now and then.

                 

                My job shifted from suite extensions to some back end system work and most recently on the CC admin UI.

                • 5. Re: Can a JSX script over-write itself?
                  CarlosCanto Adobe Community Professional & MVP

                  rats!! now I won't be able to say "we never see 'staff' in this forum"

                  • 6. Re: Can a JSX script over-write itself?
                    Muppet Mark Level 5

                    It's good to know that someone's going to keep Carlos and me in check…