0

I am trying to get a reference to a File object using the File.openDlg() method using Extendscript, but when I do this it only seems to allow me to select a specific file type. I want the dialog to allow me to open any type of file. When I use File.openDialog() I am able to select any file type, but because I am launching the OS specific file chooser when a modal dialog button is clicked, it causes the open file chooser to keep popping up--I don't know why it keeps looping, but I suspect it has to do with the "modalness" of the dialog that is currently up when the method is called. So, I am left with simply using the File.openDlg() method, but I don't understand how to inform the MacOS to allow a user to select any file type.

In Adobe's documentation the signature for the .openDlg method is as follows:

fileObj.OpenDlg ([prompt][,filter][,multiSelect])

Then it specifies that the [filter] paramter is:

In Mac OS, a filter function that takes a File instance and returns true if the file
should be included in the display, false if it should not.

So, because I do not want any filetype masking I call the method like so:

newFootageSrc.openDlg("Select your file", function(file) {return true;}, false);

This doesn't work, so I found older Adobe documentation where this was specified for the [filter] param:

In Mac OS, a string containing the name of a function defined in the current
JavaScript scope that takes a File object argument. The function is called
foreach file about to be displayed in the dialog, and the file is displayed
only whenthe function returns true

So, I simply made a named function like this

function allFileTypesOSX(file){ return true; }

And then referenced allFileTypesOSX in the method call like this:

 newFootageSrc.openDlg("Select your file", "allFileTypesOSX", false);

That didn't work, so I thought maybe just passing in the identifier itself rather than string would do the trick:

newFootageSrc.openDlg("Select your file", allFileTypesOSX, false);

But alas, that didn't work. Has anybody successfully been able to control file types in the MacOS dialog using ExtendScript?

ariestav
  • 2,799
  • 4
  • 28
  • 56

1 Answers1

3

I know I have this working in scripts at home, so I'll double check the syntax I use when I get home, but I do something along these lines (I'm supporting both windows and mac users).

var fileMask;
if(isWindows()) fileMask = '*.psd';
if(isMac()) fileMask = function(file){file.name.match(/\.psd$/i) ? true:false;}
var files = File.openDialog ('prompt', fileMask, true);

Its more similar to original attempt - you should be passing the actual filter function, not its name.

ETA: If you're not trying to actually limit the selectable files - have you tried just passing null, or leaving the parameter out altogether? They are all optional.

ETA: actual code from a working script (for Photoshop, works in CS3+). By 'works' I mean users on macs are able to select the files they need. I don't have a mac myself to actually see what they see. In a different script I found the following comment to myself above the isSupported function: 'returns true or false depending on if file is a png. 'Filter' doesn't seem to be working right on macs so this is a double check'. If the filter function isn't working as per the documentation this will definately be a problem for you when using the openDlg version of the method.

 var filter;
    if (isWindows()) {
        filter = ["PNG:*.png"];
    }
     else {     
        filter = isSupported;                   
    }                   

    win.btnAdd.onClick = function() {
        var f = File.openDialog("Open File", filter, false) ;
        if (f == undefined)
            return;
        if (isSupported(f)) {
            files = new Array();
            files[0]=f;

            win.lstImages.text = f.name;
            methodInvoker.EnableControls();
        } else {
            alert("'" + decodeURI(f.name) + "' is an unsupported file.");
        }
    };

isSupported = function(file) {
    try {
        //macs will send a file or a folder through here.  we need to respond true to folder to allow users to navigate through their directory structure
        if (file instanceof Folder)
            return true;
        else
            return file.name.match(/\.png$/i) != null;
    } catch (e) {
        alert("Error in isSupported method: " + e);
    }
}
Anna Forrest
  • 1,711
  • 14
  • 21
  • I have tried leaving them out, but since I'm using the .openDg() method rathe than the .openDialog() method, masking is happening by default on MacOS. I will try this solution later tonight. Thanks for posting. – ariestav Aug 26 '14 at 18:18