Skip navigation
Maheshwara Moorthy 39 posts
Dec 15, 2009
Currently Being Moderated

doScript

Aug 3, 2010 4:20 AM

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

 
Replies
  • Currently Being Moderated
    Aug 3, 2010 4:36 AM   in reply to Maheshwara Moorthy

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

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 3, 2010 4:48 AM   in reply to Harbs.

    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…

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 3, 2010 6:32 AM   in reply to Maheshwara Moorthy

    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…

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 18, 2010 6:52 AM   in reply to Maheshwara Moorthy

    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;
         }
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Sep 28, 2011 7:06 AM   in reply to Kasyan Servetsky

    #Kasyan Servetsky:

    is it possible to get your script work for Photoshop?

     

    Maybe Photoshop can't execute:

    app.doScript(myVbScript, ScriptLanguage.visualBasic);

     

    Thanks!

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 28, 2011 7:56 AM   in reply to cmeira

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

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2011 10:24 AM   in reply to Kasyan Servetsky

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 30, 2011 6:39 AM   in reply to pkrk

    Mine was just some typos.

     

    Thank you.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 30, 2011 8:49 AM   in reply to pkrk

    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!

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2011 10:41 AM   in reply to pkrk

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points