2 Replies Latest reply on Aug 19, 2014 3:08 AM by nicolas@paperchefs.com

    Check file type after JavaScript File.openDialog()...

    nicolas@paperchefs.com Level 1

      So, I'm on Mac OS and want my script to obviously cancel on the "Cancel" bottle, as well as check whether the file selected was a PDF. I've had some success with the code below, but some of the PDF's I select get cancelled with this script. Upon checking, the FilePath.type property returns "????" for some PDF's - anyone know why?


      var myFilePath = File.openDialog("Please choose a PDF file...", undefined, false);
      if((myFilePath == "") || (myFilePath == null)){
      else if(myFilePath.type != "PDF "){
          alert("Please choose a PDF file!") ;


      I'm quite new to scripting. Thank you!

        • 1. Re: Check file type after JavaScript File.openDialog()...
          [Jongware] Most Valuable Participant

          Under Windows, all you can do is supply a file extension (the well-known "*.PDF" filter), and it's wise to always add the catch-all "*.*" for 'All files'. That way, a user can select a file even if it has the wrong extension (and in that case, it's the user's responsibility to make sure the file is of the expected type nevertheless).


          Under OSX, there are two options:


          1. The file has the correct "Creator/Type" metadata; and/or

          2. The file has the correct file extension.


          Historically, Macintosh-based OSes used option #1, and software was not expected to look at the actual file names. However, that soon became unworkable when people tried to exchange files between their Mac and (virtually any) other systems. That lead to various tools to 'assign' Creator/Type data to well-known file extensions, just so they could be opened with the right software on a Mac. Also the reverse happened; saving a file 'without extension' on a Mac was not a problem, but to open it on Windows you often had to add the right extension.


          So, somewhere around one or two System versions ago, Apple decided to go back to the old, trusted 'File Extension' way. But OSX still saves Creator/Type metadata for files created on your own Mac. Checking the "type" should work then -- but the problem is, what if you got your file from somewhere else? The extension may be correct, but if it has no file type, your script will not see it.


          Fortunately, the Mac OSX File Open dialog allows some pretty wicked customizations. Never mind a file filter, you can run a custom function that decides for each separate file and folder whether it should be selectable or not. The function has access to all file attributes; you can go so far as to checking the file name, and reject all files that contain a 'q', or files that are read-only, or files that are smaller than 1Kb.


          Check out this piece of code (I wrote it myself, but various hints and tricks were read on this very forum):


          if (File.fs == "Windows")
              pdfFile = Folder.myDocuments.openDlg( 'Load PDF document', "Adobe PDF:*.pdf,All files:*.*", false);
              pdfFile = Folder.myDocuments.openDlg( 'Load PDF document', function(file) { return file instanceof Folder || (!(file.hidden) && (file.name.match(/\.pdf$/i) || file.type == "PDF ")); }, false );


          (I advise you to copy the Windows fragment as well, just in case your script goes 'round the world.)

          • 2. Re: Check file type after JavaScript File.openDialog()...
            nicolas@paperchefs.com Level 1

            Hey [Jongware], thanks so much for your awesome (and comprehensive!) answer!