I need to screen the filenames chosen by users to make sure that there aren’t any characters that are invalid on Mac or Windows or Dropbox. (There have been quite a few issues on this lately, particularly with files contained within compressed folders.)
I’ve seen one Mac app that replaces invalid characters typed by the user in the SaveAs dialog, while they are typing. This seems like a much better approach than rejecting the filename after the user has closed the SaveAs dialog, but I don’t see any way to do this in Xojo.
Does anyone know of declares or plugins (Mac & Windows) that would make this possible?
OS X doesn’t care about Windows or DropBox characters
And I know of no way to hook into the name entry pane in a SaveAsDialog from Xojo (with or without a plugin)
GetSaveFolderItem on Windows lets you enter anything, but it won’t confirm names containing illegal characters.
The only way would be to use a TextField first to enter the name, so you can replace, and then pass the result as default file name to GetSaveFolderItem.
It looks like Dropbox doesn’t care beyond the limitations of the OS’s. Without using a plugin, I’d get the name from the Save As dialog, filter/correct it and, if different, show a dialog warning the user and asking them if they want to fix it. If so, show the Save As dialog again with the new name for approval. Not ideal, but keep in mind that most users probably won’t see this.
This regular expression code will replace the “bad” characters with underscores:
dim rx as new RegEx
rx.SearchPattern = "[\\\\/<>:""|?*]+|[. ]+$"
rx.ReplacementPattern = "_"
rx.Options.ReplaceAllMatches = true
dim newName as string = rx.Replace( saveAsItem.Name )
if newName <> saveAsItem.Name then
...
end if
I’m just going to get the filename after they choose it, tell them why it doesn’t work, filter out the illegal characters, and give them a chance to save again. Not ideal, but it’ll work.