9 Replies Latest reply on May 26, 2010 1:33 PM by Muppet Mark-QAl63s

    File Dialog Selection?How

    Muppet Mark-QAl63s Level 4

      How do I go about doing this I have tried numerous ways n can't get my head around this one…

       

      function main() {
           var csvFile = File.openDialog('Please select your CSV File…', isCSV);
           if (csvFile != null) {
                // Read it etc.
           } else {
                // Alert etc.
           }
      }
      
      main();
      
      function isCSV(fileObj) {
           var fileName = decodeURI(fileObj.name);
           if (fileName.match(/\.csv$/i) ? csV = true : csV = false);
           return csV;
      }
      

      Now it appears to lock out 'everything' with the exception of the files ending in '.csv' for a user to open. Problem is I can't navigate folders from where the user file browser last was… There must be a way to do this properly so that I can only select '.csv' but be able to navigate the directory structure? TVM.

        • 1. Re: File Dialog Selection?How
          Paul Riggott Level 6

          Less code can be better Mark...

          function main() {
               var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
               if (csvFile != null) {
                    // Read it etc.
               } else {
                    // Alert etc.
               }
          }
           
          main();
          
          • 2. Re: File Dialog Selection?How
            Muppet Mark-QAl63s Level 4

            Paul, that may work on the PC but locks down nothing on the mac. According to my guides OSX requires a function returning true or false. Im thinking if I want to be able to do something on both OS's then…

             

            function main() {
                 if (checkOS()) {
                      var csvFile = File.openDialog('Please select your CSV File…', isCSV);
                 } else {
                      var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
                 }
                 if (csvFile != null) {
                      // Read it etc.
                 } else {
                      // Alert etc.
                 }
            }
            
            main();
            
            function isCSV(fileObj) {
                 var fileName = decodeURI(fileObj.name);
                 if (fileName.match(/\.csv$/i) ? csV = true : csV = false);
                 return csV;
            }
            
            function checkOS() {
                 if (File.fs == 'Macintosh' ? osX = true : osX = false);
                 return osX;
            }
            

            But I can't even get the mac bit to work how I would have expected? BWT would the abvove work for you I have no means of testing it?

            • 3. Re: File Dialog Selection?How
              Michael L Hale Level 5

              I don't think you can force a file type on Mac as you can on Windows.

               

              I also think the logic of your test function is wrong. They don't need the if statement. The ? is shorthand for if, so

              function isCSV(fileObj) {
                   var csV = undefined;
                   var fileName = decodeURI(fileObj.name);
                   fileName.match(/\.csv$/i) ? csV = true : csV = false;
                   return csV;
              }
              

              or

              function isCSV(fileObj) {
                   var csV = undefined;
                   var fileName = decodeURI(fileObj.name);
                   if( fileName.match(/\.csv$/i) != null ){// the same thing as the ? line spelled out
                         csV = true;
                   }else{
                         csV = false;
                   }
                   return csV;
              }
              
              1 person found this helpful
              • 4. Re: File Dialog Selection?How
                xbytor2 Level 4

                Here's how I tackle the problem in xtools. I have a function that takes the Windows-style string and returns a JS function if you're on the mac. It handles the typical cases (*.xxx) but I'll eventually get around to having it work with whatever PS can handle.

                 

                function isWindows() {
                  return $.os.match(/windows/i);
                };
                
                function createFileSelect(str) {
                  if (isWindows()) {
                    return str;
                  }
                
                  if (!str.constructor == String) {
                    return str;
                  }
                
                  var exts = [];
                  var rex = /\*\.(\*|[\w]+)(.*)/;
                  var m;
                  while (m = rex.exec(str)) {
                    exts.push(m[1].toLowerCase());
                    str = m[2];
                  }
                
                  function macSelect(f) {
                    var name = decodeURI(f.absoluteURI).toLowerCase();
                    var _exts = macSelect.exts;
                
                    // alert(name);
                
                    while (f.alias) {
                      try {
                        f = f.resolve();
                      } catch (e) {
                        f = null;
                      }
                
                      if (f == null) {
                        return false;
                      }
                    }
                
                    if (f instanceof Folder) {
                      return true;
                    }
                
                    for (var i = 0; i < _exts.length; i++) {
                      var ext = _exts[i];
                      if (ext == '.*') {
                        return true;
                      }
                      if (name.match(RegExp("\\." + ext + "$", "i")) != null) {
                        return true;
                      }
                    }
                    return false;
                  }
                
                  macSelect.exts = exts;
                  return macSelect;
                };
                
                
                // example
                var files = File.openDialog('Select an template file', createFileSelect("Template Files: *.psd"));
                
                • 5. Re: File Dialog Selection?How
                  Muppet Mark-QAl63s Level 4

                  Mike, I tried to alter my function from your second option which I had. To you first option and looks like I got somewhere in between ? I did try to put a match or instanceof Folder but that did not work for me either. X's solution does however work (no surprise there then…) I was a long way short of that in my trail and mostly error efforts. Did think it was just going to be a simple one line thing… Thanks again both of you.

                  • 6. Re: File Dialog Selection?How
                    xbytor2 Level 4
                    var files = File.openDialog('Select an CSV file', function (f) { return (f instanceof File) && f.name.match(/\.csv$/i); } );

                     

                    This should work as a one-liner

                     

                    My solution is overkill but useful for my purposes.

                     

                     

                    -X

                    1 person found this helpful
                    • 7. Re: File Dialog Selection?How
                      Muppet Mark-QAl63s Level 4

                      X, now you is just show boating… (Im convinced your Vulcan?) After changing the error I made that Mike spotted. I did manage to get the if instanceof Folder return true to work... Typical. That however that is neat…

                      • 8. Re: File Dialog Selection?How
                        Muppet Mark-QAl63s Level 4

                        You are pulling my leg X…

                         

                        var files = File.openDialog('Select an CSV file', function (f) { return (f instanceof Folder) || f.name.match(/\.csv$/i); } )
                        

                         

                        Worked…

                        • 9. Re: File Dialog Selection?How
                          xbytor2 Level 4

                          I hadn't bothered to read the original post so I missed the part about navigation.

                          I'm glad you got it straightened out.