How to set default extension in ofSystemLoadDialog

I’m implementing the file opening function using ofSystemLoadDialog.
By the way, how can I set the default extension?

An__open__file_dialog_from_a_web_browser_on_Windows_10

For example, in the image I uploaded the file extension is set to ‘ALL Files’, but I want to change it to ‘*.jpg’.
I searched several times but couldn’t find it, so I’d appreciate it if you could help me.

Hi @vani , I looked at the documentation and I don’t think there is a way to set the file extension pulldown to something specific. However, The fileOpenSaveDialogExample project in the /examples/input_output folder does show how to manage the file extension (and other related stuff) after a user has selected a file.

If you look in ofSystemUtils.cpp, you’ll see that ofSystemLoadDialog() is very specific to each os. So if you’re on windows you may be able to find a way to set the extension by modifying the windows section of the function.

i’ve recently added this to an addon i’m maintaining. GitHub - kritzikratzi/ofxNative: Native windows/mac/linux functions for openFrameworks

the signature is the same as ofxSystemLoadDialog, except there is an additional extensions parameter.

std::string windowTitle = "Load File";
bool bFolderSelection = false;
std::string defaultPath = "c:/test.jpg";
std::vector<std::string> extensions = {"jpg"}; // maybe more than one extension works, maybe not :)

ofxNative::systemLoadDialog(windowTitle, bFolderSelection, defaultPath, extensions);
ofxNative::systemSaveDialog(defaultName, windowTitle, extensions);

it’s implemented for osx and windows.

@TimChi handling the extension after the dialog has become problematic, especially when saving files. the problem is that sandboxed apps on osx have access to only the file that was specified in the system dialog. so if the user enters “save as: test”, then you are not allowed to save to “test.jpg”.

1 Like

Hey @kritzikratzi nice job with your addon! So do you feel like the sandboxing on macOS has effectively broken the functionality? If so maybe the macOS parts of these oF functions could use an overhaul at some point. An addon is a great way to get around issues, but you could also see if these changes would work if incorporated directly into oF. And then maybe vani’s idea of setting the default extension could also make its way into the core at some point. There may be some reasons why it was omitted in the first place, but it seems like it might be easy to incorporate it into oF without breaking anything.

I have an osx project that has been saving .xml files without the dialog box, so maybe I’ll add the box to get a better feel for how it working (or not working as the case may be). I’m pretty new to macOS after many years with linux.

sandboxing has broken many things :smiley:

but if you’re talking about this file: openFrameworks/ofApp.cpp at e06be9a4c61a0bb7298e17bf36f9e4f4954e76c8 · openframeworks/openFrameworks · GitHub

i think it still works as expected: if you load test.png and press save you will be suggested to save as “.png” again, but nothing will stop you from saving to “test.xml”, which in turn might be an issue for image.save().

also: i’m guessing most OF apps don’t even need or care about the sandbox on osx. (the biggest difference an active sandbox supresses the “this is a dangerous app. are you sure you want to open this???” popup.

If so maybe the macOS parts of these oF functions could use an overhaul at some point. An addon is a great way to get around issues, but you could also see if these changes would work if incorporated directly into oF.

yes. i think it doesn’t hurt that they live in an addon, it gives me the flexibility that i need myself.

but the idea is that the good, stable parts can go back into OF at any time (i mean… look at my code, it’s basically the OF source with a few minor adjustments :smiley: )

And then maybe vani’s idea of setting the default extension could also make its way into the core at some point. There may be some reasons why it was omitted in the first place, but it seems like it might be easy to incorporate it into oF without breaking anything.

yes, i also don’t see any problem there.

I have an osx project that has been saving .xml files without the dialog box, so maybe I’ll add the box to get a better feel for how it working (or not working as the case may be). I’m pretty new to macOS after many years with linux.

yes!

if you try my addon, make sure to try “ofxNative::setWindowRepresentedFilename()”. it’s a very handy osx feature. if set correctly you get filename+icon in the titlebar of the window. you can either drag&drop the icon (it’s the same as dragging the file from finder), or you can right click to see where the file is saved.

1 Like