Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Aug 18, 2021 Aug 18, 2021

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...

TOPICS
Scripting , SDK
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Aug 18, 2021 Aug 18, 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.

  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 19, 2021 Aug 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");

Untitled.png

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 19, 2021 Aug 19, 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");

saveAsFilter.PNG

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 20, 2021 Aug 20, 2021
LATEST

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();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines