Ask file: can I force the user to use an extension?

Paul Hibbert lc at pbh.on-rev.com
Tue Feb 28 16:57:40 EST 2012


On 28 Feb 2012, at 14:41, Graham Samuel wrote:

> Is there any way I can hack into the conventional Save dialog so that it thinks it is being asked to save a file with an extension in spite of my user not typing one?
> 
> I haven't thought of a workaround but maybe it will be obvious to someone on the list.


Hi Graham,

I have just been though a similar issue, but my app allows the user to save a GIF, PNG or JPEG file and I wanted to be sure the user added the correct extension. I did end up putting a check in place, with a warning dialogue, inside a repeat loop. Potentially a user could sit there all night typing the wrong extension, but the check also corrects the extension and re-presents the save dialogue, so if they continually type a wrong extension over the corrected one, they deserve to sit there all night, eventually they will learn!

I know it's a bit clumsy, but not being in a position to compete with the likes of Adobe I felt it was better than allowing the user to screw-up and end up with an un-useable file, or worse, overwrite a good file with an un-useable one. It seems the only alternative was to write a custom save dialogue routine to mimic the OS dialogue which could be a nightmare with a cross platform app.

Ken's answer is the most straightforward, but if you are mainly concerned with the user inadvertently overwriting a file, and really don't want to have a pop-up or re-present the save dialogue, you could check to see if the file exists, then just modify the name slightly. Many apps will add a version number in this case, so if they type MyOwnFile and your script adds the missing .jpg extension, but MyOwnFile.jpg already exists, then you further modify the filename to MyOwnFile-1.jpg or MyOwnFile_1.jpg. The file save routine would need to be in a checking loop so the next MyOwnFile becomes MyOwnFile-2.jpg etc., so it can only exit the loop when it has a unique filename beginning with their choice and ending with the correct extension.

Paul



More information about the use-livecode mailing list