Skip to main content
Inspiring
August 19, 2021
Question

Using File.saveDialog() to specify a default file format on MacOS

  • August 19, 2021
  • 2 replies
  • 1044 views

I'm writing a preset system for my plugin that saves .Json files to disk. The method I'm using is the File.saveDialog() command. In the documentation for the .saveDialog method, it mentions that for Windows users, I can specify a filter parameter that will make it save to a format of my choosing by default. However, this filter parameter doesn't work on MacOS.

 

So how can I specify a default file format in the save dialog on MacOS? Right now, the preset is just saving a file without any file extension...

This topic has been closed for replies.

2 replies

Disposition_Dev
Legend
August 20, 2021

Unless the user will be inputting the file name into this dialog, you could automatically save a file with whatever extension you want by prompting the user for a folder, then just building the File() object dynamically.

 

So say you know you want to be saving a JSON file, and you know you want it to be called "MY_JSON_1234.json", you can prompt the user for the folder they want the file to be saved into, then you can build your file name and then write the file.

var myJsonData = {key:"value"};
var destFolder = Folder.selectDialog("Save Location");
var myFileName = "MY_JSON";
var myFileIndex = 1234;
var myFileExtension = ".json";

var outputFile = File(destFolder.fullName + myFileName + "_" + myFileIndex + myFileExtension;

outputFile.open("w");
outputFile.write(JSON.stringify(myJsonData));
outputFile.close();
CarlosCanto
Community Expert
Community Expert
August 19, 2021

actually the filter parameter is used to show you only file types you want, not to automatically save with a specific extension.

 

Filter (optional): Data Type: any, Default Value: null

In Windows only, a filter that limits the types of files displayed in the dialog.

In Windows only, a filter expression such as "Javascript files:*.jsx;All files:*.*". Not used In Mac OS.

  

Inspiring
August 19, 2021

On Windows, the filter does act as a default file format selector, even if it was intended only to filter out files displayed.

var myFile = File.saveDialog("Save Preset", "*.json");

Notice how the 'Save as type:' dialog box is selected as '.json'

CarlosCanto
Community Expert
Community Expert
August 20, 2021

you're right, does it save as json if we don't supply an extension?

 

I didn't try, but this seems interesting

var myFile = File.saveDialog("Save Preset", "*.json; *.js");