12 Replies Latest reply on Oct 3, 2011 10:41 AM by pkrk

    doScript

    Mahesh_JW

      Hi All,

       

      This is my Code:

       

      var TemplateFile = File.openDialog("Choose the Template File");

      var Root_Folder = Folder.selectDialog("Choose the Root Folder");


      var copyPasteScript ="";
      copyPasteScript = "tell application \"Finder\"\r";
      copyPasteScript += "set dstFlChk to \""+TemplateFile+"\"\r";
      copyPasteScript += "if exists file dstFlChk then\r";
      copyPasteScript += "duplicate \""+TemplateFile+"\" to \""+Root_Folder+"\"\r";
      copyPasteScript += "end if\r";
      copyPasteScript += "end tell"

         
      app.doScript(copyPasteScript, ScriptLanguage.applescriptLanguage);

       

       

      It doesnt copy the file, I dont know what is the problem.

       

      I also tried this,

       

      1.   copyPasteScript += "copy file (\""+TemplateFile  +"\" as string) to folder (\"" + Root_Folder + " as string);

      2.   copyPasteScript += "copy file \""+TemplateFile +"\" to folder \"" +  Root_Folder+"\"\r";

      3.   copyPasteScript += "duplicate file(\""+TemplateFile+"\" as string) to folder(\""+Root_Folder+"as string)\"\r";

       

      What is the problem, plz give exact code

       

      Thanks in Advance

        • 1. Re: doScript
          Harbs. Level 6

          I'm not sure what your'e problem is, but there's no reason to use doScript()

           

          Harbs

          • 2. Re: doScript
            Muppet Mark-QAl63s Level 4

            I would agree the AppleScript is just using the mac's Finder to duplicate a file to location… No need you can do this with the File object in javascript. It's the Folder object that does not have a copy method…

            • 3. Re: doScript
              Mahesh_JW Level 1

              Thanks Mark,

               

               

              Actually my task is copy the file and paste it in a destination folder, but the thing is it should not modify the date of creation.

               

              While doing copy paste in javascript it modifies the current date to that file. It should not happen to my case.

               

              Plz help me.

               

               

              Thanks in Advance

              • 4. Re: doScript
                Muppet Mark-QAl63s Level 4

                I had NOT noticed that behavior before now… Im rather rushed at the moment but your problem lies with the file paths passed by javascript… They are POSIX in style and unescaped so Finder will have no idea what to do with it. You can specify POSIX paths & files when working with Finder but you will need to change the AppleScript strings to do so…

                • 5. Re: doScript
                  Mahesh_JW Level 1

                  Thanks Mark,

                   

                  Can u help me to the exact code. This is my Code:

                   

                  var TemplateFile = File.openDialog("Choose the Template File");
                  var Root_Folder = Folder.selectDialog("Choose the Root Folder");

                   

                  var copyPasteScript ="";
                  copyPasteScript = "tell application \"Finder\"\r";
                  copyPasteScript += "set dstFlChk to \""+File(TemplateFile).fullName.toString()+"\" as string \r";
                  copyPasteScript += "set dstFld to \""+Root_Folder+"\" as string\r";
                  copyPasteScript += "copy file dstFlChk to folder dstFld\r";
                  copyPasteScript += "end tell";

                   

                  app.doScript(copyPasteScript, ScriptLanguage.applescriptLanguage);

                   

                   

                  The error comes like this:

                  Can't set folder "~/Desktop/out" to file "~/Desktop.picture3.png"

                   

                  Thanks in advance,

                  • 6. Re: doScript
                    Kasyan Servetsky Level 5

                    Actually my task is copy the file and paste it in a destination folder, but the thing is it should not modify the date of creation.
                    While doing copy paste in javascript it modifies the current date to that file. It should not happen to my case.

                    It's a little bit late to answer -- two weeks have already passed -- but here is a function that I wrote to deal with this problem, it works both for Mac and Windows.

                     

                    Kasyan

                     

                    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 (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") {
                              if (myFolder.fsName.match("Desktop") != null) {
                                   var myMacFolder = myFolder.fsName.replace(/\//g, ":");
                                   var myMacFile = myFile.fsName.replace(/\//g, ":");
                                   var myAppleScript = 'tell application "Finder"\r';
                                   myAppleScript += 'set myFolder to a reference to folder (name of startup disk & "' + myMacFolder + '")\r';
                                   myAppleScript += 'set myFile to a reference to file (name of startup disk & "' + myMacFile + '")\r';
                                   myAppleScript += 'tell myFile\r';
                                   myAppleScript += 'move to myFolder\r';
                                   myAppleScript += 'end tell\r';
                                   myAppleScript += 'end tell\r';
                              }
                              else if (myFolder.fsName.match("Documents") != null) {
                                   var myMacFolder = myFolder.fsName.replace(/\//g, ":");
                                   var myMacFile = myFile.fsName.replace(/\//g, ":");
                                   var myAppleScript = 'tell application "Finder"\r';
                                   myAppleScript += 'set myFolder to a reference to folder (name of startup disk & "' + myMacFolder + '")\r';
                                   myAppleScript += 'set myFile to a reference to file (name of startup disk & "' + myMacFile + '")\r';
                                   myAppleScript += 'tell myFile\r';
                                   myAppleScript += 'move to myFolder\r';
                                   myAppleScript += 'end tell\r';
                                   myAppleScript += 'end tell\r';
                              }
                              else {
                                   var myMacFolder = myFolder.fullName.replace(/\//, "").replace(/\//g, ":");          
                                   var myMacFile = myFile.fullName.replace(/\//, "").replace(/\//g, ":");
                                   var myAppleScript = 'tell application "Finder"\r';
                                   myAppleScript += 'set myFolder to folder "' + myMacFolder + '"\r';
                                   myAppleScript += 'set myFile to document file "' + myMacFile + '"\r';
                                   myAppleScript += 'tell myFile\r';
                                   myAppleScript += 'move to myFolder\r';
                                   myAppleScript += 'end tell\r';
                                   myAppleScript += 'end tell\r';
                              }
                              app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage);
                         }
                         if (myMovedFile.exists) {
                              return true;
                         }
                         else {
                              return false;
                         }
                    }
                    
                    • 7. Re: doScript
                      cmeira Level 1

                      #Kasyan Servetsky:

                      is it possible to get your script work for Photoshop?

                       

                      Maybe Photoshop can't execute:

                      app.doScript(myVbScript, ScriptLanguage.visualBasic);

                       

                      Thanks!

                      • 8. Re: doScript
                        Muppet Mark Level 5

                        You are correct… Photoshop has no app.doScript()

                        • 9. Re: doScript
                          pkrk Level 1

                          I am having a surprising amount of trouble trying to get this script to work. (Error can't get file/folder) (-1728)

                           

                          I'm sure there is something simple I am missing.

                           

                          I've tried every combination of file and folder comannds i can think of...

                           

                          Javascript- mac

                           

                           

                          var myFolder=Folder("/Volumes/myserver/inthis folder/");   //folder on a server that i want to send the file

                           

                          var myFile= File("~/desktop/this folder/aaaaaa.indd");

                           

                          ... i know, this is sad

                          • 10. Re: doScript
                            pkrk Level 1

                            Mine was just some typos.

                             

                            Thank you.

                            • 11. Re: doScript
                              pkrk Level 1

                              Actually, I can't get it to work for the shared network folder. It worked fine form folder to Folder on the same station.

                               

                              The network folder is smb and should typically already be mounted.  So I don't think I need to enter username and password?

                               

                              can someone show me how to adapt that code.

                               

                              Applescript is quite a different animal than javascript!

                              • 12. Re: doScript
                                pkrk Level 1

                                I decided I don't care about the created data and just used javascript. It worked fine.

                                 

                                I still have no idea why the applescript wouldnt work for server folders for me but it's not a big deal.

                                 

                                Thanks