4 Replies Latest reply on Dec 1, 2009 2:55 PM by Muppet Mark-QAl63s

    Check if a file exists in network dir (JS)

    penyfai Level 1

      I've found an old post that has pointed me at:


      if (myFile.exists){

      alert("File Exists");



      alert("File doesn't exist");


      The problem is that when I use the path  macname/volumes/dir/folder/folder/file.indd (which was created from variables) as myFile the script doesn't find a file that I know exists. All I get is the "File doesn't exist" alert.


      Do I need to define myFile as being a string?


      I'm still fumbling my way around JS, and converting Applescripts to JS to work cross-platform. Thanks in advance....

        • 1. Re: Check if a file exists in network dir (JS)
          Kasyan Servetsky Level 5

          Do I need to define myFile as being a string?


          No, define it like so:


          var myFile = new File("/Volumes/VolumeName/FolderName/SubFolderName/FileName.indd");

          • 2. Re: Check if a file exists in network dir (JS)
            Thomas B. Nielsen Level 3

            To get the path to a file i normally use this little script:

            // JavaScript Document
            var myFile = File.openDialog("Choose a File");
            if((myFile != "")&&(myFile != null)){
                alert("Path: " + myFile + "\nType: " + myFile.type);



            Perhaps this c/p can help you:


            function mounter() {
                var myDocument = app.activeDocument;
                // we are saving the preview
                var destination = "/Volumes/myfoldername";
                //var destination = "/Users/tn/Documents/Tryksager/Design/";
                var volume = "afp://sub.dn.com/myfoldername";


                try {
                    with (myDocument) {
                        if(file_name = get_filename(name)) {
                                if (Folder(destination).exists) {
                                //alert("Folder exists: " + Folder(destination));
                                } else {
                                if (confirm ("Mount volume?", false, "Mount volume")) {
                                    // volume mounted, do what you need
                                } else {
                                    alert("Could not mount");
                } catch ( err ) {
                    // No error warning
                function mount_volume() {
                    var myScriptFolderPath = pluginPath + "/resources/"; // Path to the applescript
                    app.doScript(File(myScriptFolderPath + "mountvolume.scpt"),ScriptLanguage.applescriptLanguage);




            //The applescript for mounting volumes


            (*set user_name to ""
            set Dialog_1 to display dialog "Please enter your user name" default answer ""
            set the user_name to the text returned of Dialog_1


            set pass_word to ""
            set Dialog_2 to display dialog "Please enter your password" default answer "" with hidden answer
            set pass_word to the text returned of Dialog_2*)
            tell application "Finder"
                    mount volume "afp://mysub.mydn.com/myfolder" as user name "myusername" with password "mypassword"
                end try
            end tell




            Thomas B. Nielsen


            1 person found this helpful
            • 3. Re: Check if a file exists in network dir (JS)
              penyfai Level 1



              My saviour once again!


              Couldn't get it to work properly, but then noticed that the BasePath variable I was using had the old Applescript style paths which included the Macname.


              Took that out and it works a treat.


              Many thanks again, and also to you Thomas - I was actually going to write a subscript to mount/remount missing servers, so you've read my mind.


              Cheers both


              • 4. Re: Check if a file exists in network dir (JS)
                Muppet Mark-QAl63s Level 4

                I've recently moved from the AppleScript side and whilst I can use the do script method for mounting volumes without issue. There was also the alternative of creating aliases of my network drives. In a quick test with our works server and my idisk this worked just fine. Not sure if this is good or bad method? File.execute(); resolves this by mounting it.


                var fileRef = new File ('~/Desktop/MacRaid alias');


                if (fileRef.exists) {

                alert('File does exist…');

                if (fileRef.alias) {

                alert('File is Alias? = True…');

                // Passwords are in Keychain… for NO dialog



                else {

                alert('File is Alias? = False.');



                else {

                alert('File does NOT exist?');


                1 person found this helpful