10 Replies Latest reply on Sep 23, 2016 6:02 AM by Trevorׅ

    Modification Date changed

    tpk1982 Level 4

      Hi,

       

      I am trying to create folders and copy the file what i pick. The below code is working fine. But the copied file modified date is changed to current date and time. Is it possible to retain the old time and date?

       

      function myfolder(){
          var _files = File.openDialog("Select Indesign/Illustrator file","*",true);
      if(_files==null){return;}
      myFolder = Folder.selectDialog("Choose Main folder to created subfolders");    
      if(myFolder==null){return;}
      _parent = _files[0].parent.toString().replace(_files[0].parent.parent,"");  
      var mygetname=prompt("Please enter the Event ID","");
      if(mygetname==null){return;}
      var myartname=prompt("Please enter the Part Number","");
      if(myartname==null){return;}
      var myartnamew=myartname+"_Aa"
      new Folder(myFolder + "/" + mygetname).create();  
      new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew).create();  
      new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Aa").create();
      new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew +"/Soft").create();
      _files[0].copy(new File(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Aa"+ "/"+myartnamew+ "/" ));
      }
      myfolder();
      

       

       

      Thanks,

      K

        • 1. Re: Modification Date changed
          Trevorׅ Adobe Community Professional

          Hi

           

          See my answer from here Re: copy file to folder in applescript inside javascript

          The answer for Both Mac and Windows.

           

          unction MoveFile(myFile, myFolder) {  
              if (!myFile instanceof File || !myFolder instanceof Folder || !myFile.exists || !myFolder.exists) return false;  
              var myMovedFile = new File(myFolder.absoluteURI + "/" + myFile.name);  
              if (myMovedFile.exists) return false;  
              if (File.fs == "Windows")  {  
                  var myVbScript = 'Set fs = CreateObject("Scripting.FileSystemObject")\r';  
                  myVbScript +=  'fs.MoveFile "' + myFile.fsName + '", "' + myFolder.fsName + '\\"';  
                  app.doScript(myVbScript, ScriptLanguage.visualBasic);  
              }  
              else if (File.fs == "Macintosh") {  
                  var myAppleScript =   
                  'tell application "Finder"\r' +  
                  'move POSIX file "' + myFile.fsName + '"  to POSIX file "' + myFolder.fsName + '" with replacing\r' +  
                  'end tell\r'  
                  app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage);  
              }  
              if (myMovedFile.exists) {  
                  return true;  
              }  
              else {  
                  return false;  
              }  
          }  
          

           

          HTH

           

          Trevor

          1 person found this helpful
          • 2. Re: Modification Date changed
            Trevorׅ Adobe Community Professional

            P.s.

            Change this line

            if (!myFile instanceof File || !myFolder instanceof Folder || !myFile.exists || !myFolder.exists) return false;   

            to

            if (!myFile instanceof Folder || !myFolder instanceof Folder || !myFile.exists || !myFolder.exists) return false;   

            To have it work for copying folders

             

            P.P.s

             

            Test it first on a test folder to make sure no harm comes

            • 3. Re: Modification Date changed
              tpk1982 Level 4

              Hi Trevor,

               

              Thank you for the coding. But i tried this to include in my script, it is not working. Please suggest where i missed?

               

              function myfolder(){
                  var _files = File.openDialog("Select Indesign/Illustrator file","*",true);
              if(_files==null){return;}
              myFolder = Folder.selectDialog("Choose Main folder to created subfolders");   
              if(myFolder==null){return;}
              _parent = _files[0].parent.toString().replace(_files[0].parent.parent,""); 
              var mygetname=prompt("Please enter the Event ID","");
              if(mygetname==null){return;}
              var myartname=prompt("Please enter the Part Number","");
              if(myartname==null){return;}
              var myartnamew=myartname+"_Aa"
              new Folder(myFolder + "/" + mygetname).create(); 
              new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew).create(); 
              new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Aa").create();
              new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew +"/Soft").create();
              var myFolder=(new File(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Aa"+ "/"+myartnamew));
              MoveFile(_files[0], myFolder)
              }
              myfolder();
              
              
              
              
              function MoveFile(myFile, myFolder) { 
              if (!myFile instanceof File || !myFolder instanceof Folder || !myFile.exists || !myFolder.exists) return false;    
              
               
                  var myMovedFile = new File(myFolder.absoluteURI + "/" + myFile.name); 
                  if (myMovedFile.exists) return false; 
                  if (File.fs == "Windows")  { 
                      var myVbScript = 'Set fs = CreateObject("Scripting.FileSystemObject")\r'; 
                      myVbScript +=  'fs.MoveFile "' + myFile.fsName + '", "' + myFolder.fsName + '\\"'; 
                      app.doScript(myVbScript, ScriptLanguage.visualBasic); 
                  } 
                  else if (File.fs == "Macintosh") { 
                      var myAppleScript =   
                      'tell application "Finder"\r' + 
                      'move POSIX file "' + myFile.fsName + '"  to POSIX file "' + myFolder.fsName + '" with replacing\r' + 
                      'end tell\r' 
                      app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage); 
                  } 
                  if (myMovedFile.exists) { 
                      return true; 
                  } 
                  else { 
                      return false; 
                  } 
              } 
              
              • 4. Re: Modification Date changed
                tpk1982 Level 4

                Hi Trevor,

                 

                Any suggestions? Also i need to duplicate the file and rename it. I achieved all those with Javascript except this time change

                 

                I am a newbie for Apple script and dont know how to include with Javascript.

                 

                Thanks,

                K

                • 5. Re: Modification Date changed
                  Trevorׅ Adobe Community Professional

                  Perhaps the easiest way for you is the move the files / folders with jsx recording there current dates before the move and then restore the dates with the function I wrote below.

                  I didn't test it but it hopefully it should work.

                   

                  $.level = 1;
                  
                  function changeDate(file, date) {
                      var year, month, dayOfMonth, hours, minutes, command, appleDate;
                  
                      year = 1900 + date.getYear();
                      month = pad(1 + date.getMonth());
                      dayOfMonth = pad(date.getDate());
                      hours = pad(date.getHours());
                      minutes = pad(date.getMinutes());
                      appleDate = "" + year + month + dayOfMonth + hours + minutes;
                  
                      command = ["try",
                          "do shell script \"touch -ft " + appleDate + " '" + file + "'\"",
                          "end try"
                      ].join('\n');
                      try {
                          app.doScript(command, ScriptLanguage.APPLESCRIPT_LANGUAGE);
                      } catch (e) { $.writeln(command + "\n" + e); }
                      return undefined;
                  
                      function pad(value, digits) {
                          value = "" + value;
                          digits = digits || 2;
                          var padding = new Array(digits + 1).join("0");
                          return (padding + value).slice(value.length);
                      }
                  }
                  
                  // changeDate(new File('path/to/file').fsName, new Date('1 Jan 2000'));
                  

                   

                  HTH

                   

                  Trevor

                  • 6. Re: Modification Date changed
                    tpk1982 Level 4

                    Hi Trevor,

                     

                    It is not working as expected. Is it am applying your code correctly? Please take a look.

                     

                    $.level = 1;  
                    
                    
                    function myfolder(){  
                        var _files = File.openDialog("Select Indesign/Illustrator file","*",true);  
                    if(_files==null){return;}  
                    myFolder = Folder.selectDialog("Choose Main folder to created subfolders");      
                    if(myFolder==null){return;}  
                    _parent = _files[0].parent.toString().replace(_files[0].parent.parent,"");    
                    var mygetname=prompt("Please enter the Event ID","");  
                    if(mygetname==null){return;}  
                    var myartname=prompt("Please enter the Part Number","");  
                    if(myartname==null){return;}  
                    var myartnamew=myartname+"_Aa"  
                    new Folder(myFolder + "/" + mygetname).create();    
                    new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew).create();    
                    new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Aa").create();  
                    new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew +"/Soft").create();  
                    changeDate(new File(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Aa"+ "/"+myartnamew+ "/" ).fsName, new Date('1 Jan 2000'));
                     _files[0].copy(new File(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Aa"+ "/"+myartnamew+ "/" ));  
                    
                     
                    
                    
                    }  
                    myfolder(); 
                    
                    
                      
                    function changeDate(file, date) {  
                        var year, month, dayOfMonth, hours, minutes, command, appleDate;  
                      
                        year = 1900 + date.getYear();  
                        month = pad(1 + date.getMonth());  
                        dayOfMonth = pad(date.getDate());  
                        hours = pad(date.getHours());  
                        minutes = pad(date.getMinutes());  
                        appleDate = "" + year + month + dayOfMonth + hours + minutes;  
                      
                        command = ["try",  
                            "do shell script \"touch -ft " + appleDate + " '" + file + "'\"",  
                            "end try"  
                        ].join('\n');  
                        try {  
                            app.doScript(command, ScriptLanguage.APPLESCRIPT_LANGUAGE);  
                        } catch (e) { $.writeln(command + "\n" + e); }  
                        return undefined;  
                      
                        function pad(value, digits) {  
                            value = "" + value;  
                            digits = digits || 2;  
                            var padding = new Array(digits + 1).join("0");  
                            return (padding + value).slice(value.length);  
                        }  
                    }  
                    

                     

                     

                    Thanks,

                    K

                    • 7. Re: Modification Date changed
                      Trevorׅ Adobe Community Professional

                      You need to apply the change date to the copy also.

                      Or does new File(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Aa"+ "/"+myartnamew+ "/" ) also not change it's date?

                      • 8. Re: Modification Date changed
                        tpk1982 Level 4

                        Hi Trevor,

                         

                        Thank you so much for the code. It is working with the added new line 16. I missed to get the modified date, now it is working fine

                         

                        function myfolder(){  
                            var _files = File.openDialog("Select Indesign/Illustrator file","*",true);  
                        if(_files==null){return;}  
                        myFolder = Folder.selectDialog("Choose Main folder to created subfolders");      
                        if(myFolder==null){return;}  
                        _parent = _files[0].parent.toString().replace(_files[0].parent.parent,"");    
                        var mygetname=prompt("Please enter the Event ID","");  
                        if(mygetname==null){return;}  
                        var myartname=prompt("Please enter the Part Number","");  
                        if(myartname==null){return;}  
                        var myartnamew=myartname+"_Aa"  
                        new Folder(myFolder + "/" + mygetname).create();    
                        new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew).create();    
                        new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Art").create();  
                        new Folder(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew +"/Soft").create();  
                        mymodified=_files[0].modified;
                        myfile=new File(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Art"+ "/"+myartnamew+ "/" );
                        _files[0].copy(new File(myFolder + "/" + mygetname+ "/OT"+ "/" + myartnamew+ "/Art"+ "/"+myartnamew+ "/" ));  
                        changeDate(myfile.fsName, mymodified);
                        }  
                        myfolder();
                        
                        
                        function changeDate(file, date) {  
                            var year, month, dayOfMonth, hours, minutes, command, appleDate;  
                          
                            year = 1900 + date.getYear();  
                            month = pad(1 + date.getMonth());  
                            dayOfMonth = pad(date.getDate());  
                            hours = pad(date.getHours());  
                            minutes = pad(date.getMinutes());  
                            appleDate = "" + year + month + dayOfMonth + hours + minutes;  
                          
                            command = ["try",  
                                "do shell script \"touch -ft " + appleDate + " '" + file + "'\"",  
                                "end try"  
                            ].join('\n');  
                            try {  
                                app.doScript(command, ScriptLanguage.APPLESCRIPT_LANGUAGE);  
                            } catch (e) { $.writeln(command + "\n" + e); }  
                            return undefined;  
                          
                            function pad(value, digits) {  
                                value = "" + value;  
                                digits = digits || 2;  
                                var padding = new Array(digits + 1).join("0");  
                                return (padding + value).slice(value.length);  
                            }  
                        }  
                          
                        
                        

                         

                        Thanks,

                        K

                        • 9. Re: Modification Date changed
                          tpk1982 Level 4

                          HI Trevor,

                           

                          One small clarification. If i use this script in Illustrator it throws error for applescript. Is it we need to change anything?

                           

                          I hope doscript wont work in Illustrator

                           

                          Thanks,

                          Karthi

                          • 10. Re: Modification Date changed
                            Trevorׅ Adobe Community Professional

                            Hi Karthi

                             

                            It won't work with Illustrator as you can't use app.doScript with Illustrator.

                            You can either make an extension for Illustrator and use Nodejs to run the applescript, which I don't think your up to.

                            Or you can use bridgetalk to get send the script to ID to run it.

                            If it's your own system there are other methods but they are quite complicated.

                             

                            Sorry, no easy canned solution.

                             

                            Trevor