File name extension

Seem to be circling back to this but with a different problem.

If the user deletes my suggested filename including the extension the file is saved out as a plain text file with no extension to associate it back to the program.
What am I missing?
Or do you have to validate their entry and add the extension back on? If so at what stage and where do I do that?

I present the SaveAs Dialog

DIM CWPFileTypes as new FileType
CWPFileTypes.Name = “CrosswordPlayerFile”
CWPFileTypes.Extensions = “.cwptext”

dialog = New saveAsDialog
dialog.Filter = CWPFileTypes
dialog.initialDirectory = SpecialFolder.Documents
dialog.PromptText = “Select a location to save your Crossword Player file”
dialog.Title = “Save As”
dialog.SuggestedFileName = “Untitled.cwptext”
dialog.CancelButtonCaption = “Cancel”
dialog.ActionButtonCaption = “Save It!”

savevolfile = dialog.ShowModal

In my experience you cannot rely on a filetype enforcing a file extension on a Mac.
Check the filename once you have it, something like this:

if not (right(upper(,7) = ".CWPTEXT" ) then = + ".CWPTEXT" end if

Do not modify the filename after the user has provided a location via save dialog.
That’s just poor user experience, and disrespectful to users as well.

Plus, it’d be worth testing if that even works when translocated or sandboxed. Remember you only get access to the location the user has specified. Adding to the filename might hopefully change that. Terrible behaviors like the described are the reason Apple has to keep getting stricter with security.

You can check that the filename isn’t going to be problematic, but DO. NOT. CHANGE. IT.

I had tried Jeff’s approach prior but it doesn’t work.
Just wondering if this is a common problem trying to set the right extension on mac and windows, that is if users delete the extension when saving what happens?

If I set the following it associates the file to the application correctly event though there is no extension attached to the file name but this isn’t an option on windows
savevolfile.MacCreator =
savevolfile.MacType =

Have I created a problem or is it normal behaviour to be able to save files out that are potentially disocciated?

If I examine saving a Xojo project it allows you to edit the extension name but when you save it, it appends the full extension on the name.


At what point in the save process can I do this?

This line of code has been working fine for me for the last 5 years:

if right(file.Name.uppercase,4) <> ".PDF" then file.Name = + ".pdf" //oct 2013

I spent years before that providing pointless support to people who angrily emailed to tell me that their saved files could not be found.
I’d rather add the extension and save myself a lot of support time.
To me, helping the user by adding the missing extension when saving is expected behavior.
If there comes a time when Apple clamp down on this, then I’ll stop, or maybe we will get a cross platform SaveDialog that doesn’t enable the save button until the filename is OK

Tim’s Advice
But if the alternative is ‘tell the user that their suggested filename is not good enough’ ,that also sounds like a poor user experience to me.

I’ll be giving this some thought over the next few days myself.

If I do not write the extension for a filename, I expect the application taking care of that. Adding the necessary extension automaticly by the application will be appreciated by most users, especially those in a hurry.


Here is the code which will add the extension .PDF if the user did not wrote it :

[code]Dim dlgSave As New SaveAsDialog
Dim f_Test As FolderItem
Dim tosFileWrite As TextOutputStream
Dim strTemp As String

dlgSave.InitialDirectory = SpecialFolder.Documents
dlgSave.PromptText = “This is prompt text”
dlgSave.SuggestedFileName = “ThisIsMyFile”
dlgSave.Title = “Example dialog how to save”
f_Test = dlgSave.ShowModal
strTemp = f_test.NativePath
if UpperCase(Right(strTemp, 4)) <> “.PDF” Then strTemp = strTemp + “.PDF”
f_Test = GetFolderItem(strTemp)
tosFileWrite = TextOutputStream.Create(f_Test)

First the program ask for a name. When the user click save then the native path is stored in string variable “strTemp” after which it is checked if it has the necessary extension. After that the file is actually created and saved to disk.

This is only an example and is striped from the necessary tests and processing. What you find after running this code is just a file on the location of your choice.

Hope this helps.


Just to add some more information.

The file is not actually saved after pressing “Save” in the Save As dialog box. So you can test in the following way what the user choosed :

If f_Test then
// Boolean result of Save As dialog is true, so the user pressed “Save”.
// Do your save actions here
// The boolean result of Save As dialog is false, the user pressed “Cancel”
// Do here the cancel actions you want to perform
end if

Also in case the user omitted the extension, you can correct or add it in the “true” region of the “if” block.


A simple reminder seems to be nice to add:

Extensions are invisible by default (on both macOS and Windows, I forgot the visibility status on Linux).

You (the user) cannot add something you do not see.

only in Finder display… anywhere the path is extract (ie. Xojo) it is fully visible and avaliable

I wanted to hilight the fact the user may not know about the extension because by defaule it is not displayed.

And, yes, the extension is visible elsewhere (even when the preference is set to hide the extensions ?).

The other problem users have is… they believe that it is enough to change a file extension to “change the file contents” (replace the avi with mp4 for example). It works, lauch VLC and display the movie, but it does not change the file contents, it just mistake the OS and the Application(s). The data did not changed at all.

Urban legends; and far from the op question.

I think to avoid possible future problems it‘s OK to add a file extension. This does not modify the *file‘s name“.

As the developer, it is always a good idea to check if the file the application expect has the correct extension. If so, just proceed. If not, your application should try to find out what is wrong. Is it the file it expect, if so, change or add the correct extension. If it is an unexpected file, see if there is a way to retrieve the information or inform the user about the problem.

Xojo is very much capable of artificial intelligence (AI), so implement it in your applications. Your users should be very grafefull for that.