What does the Filter property do in SaveFileDialog?

When a save file dialog opens I want the suggestedname selected and the extension unselected (standard behavior in macOS). I read that to do this you set the dialog’s Filter property to the extension. But that’s not working for me. This is an example modified from the LR

Var dlg As New SaveFileDialog
dlg.SuggestedFileName = “Suggested Filename.txt”
dlg.Filter = “txt”
f = dlg.ShowModal

The entire SuggestedFileName is selected. I’ve played with endless variants with the same result (use a FileType.extension instead of a literal, use “.txt”, etc.) with the same result. The entire SuggestedFileName is selected.

Of course I could leave the extension out of the suggested name and add it back when the file is saved, but I want the user to be able to see it as visual feedback.

What am I missing?

The most reliable way to work around this folder dialog bug is to define your file type as an object in code immediately before using it.

var ftTxt as new FileType
ftTxt.Name = "Plain Text"
ftTxt.Extensions = ".txt"

var dlg as new SaveAsDialog
dlg.Filter = ftTxt
dlg.SuggestedFileName = "Suggested.txt"

var ftResult as FolderItem = dlg.ShowModal

Thanks, Tim, but when I run your code the name is just Suggested (no .txt). I could do that of course by making SuggstedFilename = “Suggested”, but I do want .txt to be visible to the user.

In later versions of macOS, when the FileType Filter is working correctly, the user cannot see the extension. Super friendly, right?

It’s not just a visual indication, as some behaviours would also break (like the “There’s already an item named “file.txt”. Would you want to replace it?” message would no longer appear when appropriate), so you’d probably not want to do that alone.

Wow… What’s the purpose of that? They try to hide the extension to make it appear like when it was not needed at all in MacOS classic?

Is there still something logical in later versions of MacOS? :thinking: :man_shrugging:

I’m happy to stay behind, on Mojave, when I see what they’re doing now. OTH, I’ve never read this behaviour before.

You don’t need to stay behind. You just need to update to the modern way of doing things.

Add a File Type Set to your project and define the different types that your app can create and open.

To simplify things, you can even drop existing files from the finder into the editor and it’ll ask the OS for the correct configuration.

Then instead of just using random strings in the filter property, you use the file types.

2 Likes

I’ve been thinking about doing a blog post about this. I guess it’s time.

2 Likes

@Greg_O_Lone The problem I posted at the beginning isn’t one of FileTypes. It’s that setting the filter property to a particular FileType in a Save (not Open) FileDialog simply hides the extension. What I was after was the standard (or at least extremely common) macOS behavior that the name is selected and the extension is shown but not selected. If that’s possible a blog post would be most welcome.

Oh, nice. I didn’t know that.

Here’s the result for a .sqlite db file:

Thank you Greg for the tip.