Save a file with filename prompt from script? I'm trying to write a simple script to save without Blender showing the confirmation popup. This works fine if the file already has a name, however, if I call bpy.ops.wm.save_mainfile() and the file has not been previously saved, Blender simply crashes/exits completely.

So is there a way to call save_mainfile() so that it behaves the same at the normal File/Save operation where it will prompt the user for a filename? (Note that calling save_as_mainfile() also crashes Blender.

Here's what I was at least hoping would work:

def execute( self, context ):
    if not bpy.data.filepath:
        bpy.ops.wm.save_mainfile( ?parameter to prompt for filename? )
    else:
        bpy.ops.wm.save_mainfile()
    self.report( { 'INFO' }, 'File saved.')
    return { 'FINISHED' }
After a bit of research, I found a solution for you.

To fix your issue, simply pass the value 'INVOKE_AREA' into save_mainfile, bpy.ops.wm.save_mainfile('INVOKE_AREA'). This will open a file selector if the file is not saved, otherwise it will bring up a prompt that confirms the user wishes to save over the file. If you want to avoid the confirm dialog, you will need to call save_mainfile without 'INVOKE AREA'.

Passing 'INVOKE AREA' defines the operator context, which allows save_mainfile to bring up the file selector.