Skip to main content
Stephen Marsh
Community Expert
Community Expert
November 25, 2020
Answered

Open Selected Files

  • November 25, 2020
  • 3 replies
  • 1385 views

I know how to open a single file from a dialog (code snippet):

 

var selectFile = app.openDialog();

 

I also know how to open a folder and get all the files in that top-level folder (code snippet): 

 

var selectFolder = Folder.selectDialog("select folder");

 

However, I can't find or work out how to have the user select multiple files (but not all files). Can somebody please enlighten me? Thank you in advance!

This topic has been closed for replies.
Correct answer Shulipa Bernad

A few weeks ago I needed this feature and found something that solved my problem on a javascript forum on the internet.
For me he decided:

selectFile = File.openDialog("please select files", Multiselect = true);
alert(decodeURI(selectFile))

3 replies

Participant
October 14, 2023

Please send me a screen recording 

Stephen Marsh
Community Expert
Community Expert
October 15, 2023
quote

Please send me a screen recording 


By @Manoj32824813540j

 

You have replied to a 3-year-old topic with an incomplete sentence.

Shulipa Bernad
Shulipa BernadCorrect answer
Inspiring
November 26, 2020

A few weeks ago I needed this feature and found something that solved my problem on a javascript forum on the internet.
For me he decided:

selectFile = File.openDialog("please select files", Multiselect = true);
alert(decodeURI(selectFile))
Stephen Marsh
Community Expert
Community Expert
November 28, 2020

Thank you Shulipa Bernad for your reply!

 

You wouldn't believe how many hours it took me to make that work, I don't "know" JS so it can take me some time and a lot of frustration to get there!

 

The following appears to work on both Mac and Win:

 

 

selectFile = File.openDialog("Please select the file/s:", Multiselect = true);

for (var i = 0; i < selectFile.length; i++) {
    var openFiles = app.open(File(selectFile[i]));
}

 

 

My next stumbling block is getting .sort to work correctly...

c.pfaffenbichler
Community Expert
Community Expert
November 28, 2020

What kind of array are you trying to sort?  

c.pfaffenbichler
Community Expert
Community Expert
November 25, 2020

Does this help? 

var aFile = selectFile(true)
alert (aFile.join("\n"));
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////// select file //////
function selectFile (multi) {
if (multi == true) {var theString = "please select files"}
else {var theString = "please select one file"};
if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog (theString, '*.jpg;*.tif;*.psd;*.png', multi)}
else {var theFiles = File.openDialog (theString, getFiles, multi)};
////// filter files  for mac //////
function getFiles (theFile) {
    if (theFile.name.match(/\.(jpg|tif|psd|png)$/i) || theFile.constructor.name == "Folder") {
        return true
        };
	};
return theFiles
};
Stephen Marsh
Community Expert
Community Expert
November 25, 2020

Thank you, it might help when I can give it some time, I'm going to have to work at it though as it is not "out of the box". At least I can see that unlike opening a file or a folder full of files, it is not cross-platform and is more complex than 1-3 lines of code.

c.pfaffenbichler
Community Expert
Community Expert
November 26, 2020

If you only work on one platform you can limit the code considerably, on windows it comes down to 

 

var theFiles = File.openDialog (theString, '*.jpg;*.tif;*.psd;*.png', true)

 

I guess.