Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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'
Copy link to clipboard
Copied
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");
Copy link to clipboard
Copied
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();
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more