Skip to main content
Mahesh_JW
Inspiring
August 3, 2010
Answered

doScript

  • August 3, 2010
  • 1 reply
  • 3573 views

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

This topic has been closed for replies.
Correct answer Kasyan Servetsky

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


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;
     }
}

1 reply

Harbs.
Legend
August 3, 2010

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

Harbs

Muppet_Mark-QAl63s
Inspiring
August 3, 2010

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…

Mahesh_JW
Mahesh_JWAuthor
Inspiring
August 3, 2010

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