Specifying initial directory/file name in file save dialog?

How do I specify the initial directory and file name in a FileChooser save dialog? Suppose I want the initial text to be “MyFile.text”, and I want the initial directory to be the desktop. Here’s what I’m doing currently:

FileChooser myChooser (
    T("Save File As"),
    (File::getSpecialLocation(File::userDesktopDirectory)).getChildFile("MyFile.text"),
    T("*.txt"));

if(myChooser.browseForFileToSave(true))
... do stuff ...

On Windows, this seems to work fine, but on the Mac this doesn’t seem to do anything. The initial directory is always whatever the user last navigated to the last time the save dialog was shown, and the initial filename is always “untitled”. This happens regardless of whether MyFile.txt already exists or not.

Am I doing something dumb?

No, you’re not doing anything wrong there.

Just one thing to check: does this file and its parent folder exist? The mac might be deciding to use a default location if it doesn’t exist.

I thought that might be it, but the behavior seems to be the same whether or not the file and/or parent folder exist.

Well, the file does get passed into the OS code, so not sure why it’s not getting used. I need to re-write all that code in obj-C shortly anyway, so not much point me spending time debugging it now.

Blast from the past… I just looked into this a bit more. Turns out the filename is not passed to the options struct to tell the dialog to use that as the default filename in the save dialog. Here’s the fix if anyone is interested.

In juce_mac_FileChooser.cpp, in FileChooser::showPlatformDialog(), look for this:

CFStringRef message = PlatformUtilities::juceStringToCFString (title);

After that insert this:

if (isSaveDialogue)
	options.saveFileName = PlatformUtilities::juceStringToCFString (currentFileOrDirectory.getFileName());

And then at the end of the function, look for this:

After that insert this:

if (isSaveDialogue)
	CFRelease (options.saveFileName);

I have the same issue, but on revision 690 so jimw’s solution won’t work. I’m sending in proper full path, and it is the default name in the save dialog on XP/Vista, but not on Mac. I’ve looked at the code vs. Apple’s documentation, but I can’t find the problem with the code.

Any ideas, or is this perhaps fixed in a later revision?

Well, trying the ‘save as’ box in the jucer, it all seems to work absolutely fine to me…

Perhaps the mac just ignores the filename you give it if that file doesn’t actually exist?

I’m wondering if this issue has ever been resolved. I am trying to populate the textfield in the Filechooser with a default filename as follows, but nothing happens. the field is blank on OS X:

    StringRef sr = serverAppInterface->getSessionID().toISO8601(false) + ".mid";
    FileChooser fc("Select a location",
                   File::getSpecialLocation(File::userMusicDirectory).getChildFile(sr),
                   "*.mid;",
                   true);

The idea is that when the FileChooser window pops up, the name of the file is already populated and selected, and if the user wants to, they can type their own, or just press Return and use the given filename.

Solved!!!

String path = File::getSpecialLocation(File::userMusicDirectory).getFullPathName();
#if JUCE_MAC
    path += "/";
#elif JUCE_WINDOWS
    path += "\\";
#endif
    path += Time::getCurrentTime().toString(true, true);
    DBG( "path: " + path );
    FileChooser fc("Select a location",
                   File(path),
                   "*.mid;",
                   true);

@jules @ed95 perhaps we can add a “withDefaultFileName” parameter to the FileChooser constructor?
somethin’ like:

    FileChooser (const String& dialogBoxTitle,
             const String& initialDirectory=File::getSpecialLocation(File:: userDesktopDirectory).getFullPathName(),
             const String& initialFilename=String("Untitled"),
             const String& filePatternsAllowed = String(),
             bool useOSNativeDialogBox = true,
             bool treatFilePackagesAsDirectories = false);

use:

path += File::separatorString;

Rail

1 Like

NEVER concatenate filenames manually!

auto path = File::getSpecialLocation (File::userMusicDirectory)
              .getChildFile (Time::getCurrentTime().toString (true, true));

FileChooser fc("Select a location", path, "*.mid;", true);
1 Like

I knew if i commented some bad code ideas you’d eventually chime in with the right approach! :grin:

1 Like