5 Replies Latest reply on May 8, 2014 3:36 AM by Trevorׅ

    How to save a copy of file? (need simple javascript)

    tazotodua

      I am noob, so need a Javascript script/install for InDesign CC,  that runs automatically when any file is opened, and saves a copy of the document to a specific folder automatically, with current time (i.e. "c:/folder/filename_COPY_6_MAY_2014_23_59_59").

      is this code right for this job?:

      var dmd = app.activeDocument;
      var oldDocName = dmd.name;
      //make new file
      var newDocName = oldDocName.substring(0,oldDocName.length-5); //removes ".indd"
      var dd = new Date;
      var NewFile = new File("/c/backups_folder/" + newDocName + dd.getDate() + "_" + dd.getMonth() +  "_" + dd.getHours() + "-" + dd.getMinutes() + "-" + dd.getSeconds() +".indd");
      //copy
      oldDocName.copy(NewFile);
      
      
      
      
      
      
      
      
      
      
        • 1. Re: How to save a copy of file? (need simple javascript)
          S Hopkins Adobe Community Professional

          Note: The following assumes that the document to be duplicated has been saved. You might make the following part of a script that creates the original document and saves it, or as part of a script that saves the original document. Notice that this example saves the current document with the new name in the backup folder and then reopens the original document. Remember that months are indexed from 0, so May is 4 not 5 (you may want to add 1 to the month index just to make it more intuitive to humans).

           

          var dmd = app.documents.item(0);

          var oldFilePath = dmd.fullName;

          var oldDocName = dmd.name; 

          var dd = new Date;

          var strInd = oldDocName.indexOf(".indd");

          var newName = oldDocName.substr(0,strInd);

          newName += (dd.getDate() + "_" + dd.getMonth() + "_" + dd.getHours() + "-" + dd.getMinutes());

          var folderRef = "~/Desktop/backup";

          var filePath = folderRef + "/" + newName + ".indd";

          dmd.save(new File(filePath));

          dmd.close();

          var docRef = app.open(File(oldFilePath));

          • 2. Re: How to save a copy of file? (need simple javascript)
            Trevorׅ Adobe Community Professional

            Why aren't you using myDoc.saveACopy(myFile) ?

            • 3. Re: How to save a copy of file? (need simple javascript)
              Laubender Adobe Community Professional & MVP

              @Trevor – doing this automatically after a file is opened would require a script with an event handler that is watching the AFTER_OPEN event. That script would be placed in a startup scripts folder.

               

              But as explained and discussed elsewhere in this forum, this is problematic. Do not remember the details, but you could search for threads with the keyword AFTER_OPEN.

               

              Uwe

              • 5. Re: Re: How to save a copy of file? (need simple javascript)
                Trevorׅ Adobe Community Professional

                Hi All,

                 

                I don't think that the link you provided on stackflow answers your problem and certainly not the best way.

                S Hopkins answer seems for sure not correct (sorry )

                 

                I think my script below is the best method.  Won't cause annoying delays in file opening.

                 

                Put it in your startup script folder

                 

                Trevor

                 

                // https://forums.adobe.com/message/6363852#6363852
                // By Trevor
                #targetengine document_copy
                (function () {
                    if (app.eventListeners.itemByName ("saveCopy").isValid) app.eventListeners.itemByName ("saveCopy").remove(); // makes debugging easier and prevents multiple eventListener creations
                    var mySaveCopyEventListener = app.eventListeners.add("afterOpen", saveCopy);  
                    mySaveCopyEventListener.name = "saveCopy";
                }) ();
                
                function saveCopy(ev)  {
                    if (ev.target.constructor.name !== 'Document') return;
                    saveCopy.Folder = "C:/backups_folder/"; // Change as needed   
                    if (!Folder (saveCopy.Folder).exists) {
                        Folder (saveCopy.Folder).create();
                        $.sleep(300);
                    }
                    if (!ev.fullName.copy (new File (
                        saveCopy.Folder + // folder name
                        ev.fullName.name.replace(/.[^.]*$/,"") + // file name without suffix
                        (" " + new Date).replace (/:/g,"\xB7").replace(/\s\S+$/,"") + // date Change format as desired
                        (ev.fullName.name.match(/\..+$/) ? ev.fullName.name.match(/\..+$/).toString() : ".indd") // suffix could be idml or who know what
                        ))) alert ("Something messed up, a copy was not made");
                }