The following is TL;DR…
A bit ago, I had a need for a “directory chooser” standard dialog to appear in one of my PythonScripts. I shared this need with the guru of all things Python (@Ekopalypse , although he will shrug off that moniker) and he graciously pursued adding code to support that in his Little Dialog Wrapper project.
Because a directory-chooser is really just a specialized “file open” standard dialog, Eko also provided that capability – great. So now the library had file-open and dir-chooser capability. But…the glaring omission to complete the set was a “file save” standard dialog. Well…of course I nudged Eko to write that part as well, but I think he simply saw no need for it and thus didn’t do it. And so of course I said I’d do it.
But much time passed, and I didn’t do it, because I didn’t exactly have a burning need. I already had some script code I’d developed for file-save based on some ancient code called EasyDialogs. And wow, checking out that link right now, it is really ancient (circa 2007)! This approach worked, but the type of standard dialogs generated by EasyDialogs are really “old school”.
So then along comes this thread (the one we’re talking in right now). The OP’s need is one that can be easily scripted…IF a nice file-save dialog could be generated.
So, I got on the proverbial horse and added the file-save dialog code to Eko’s library (in the end not a big effort, since the earlier code provided a lot of inheritable stuff). It’s been ACCEPTED and now the library offers the full complement of file-open, file-save, and directory-chooser standard dialogs.
Ok, so here’s a (PS3) script called FileSaveReplacement.py that meets the need proposed at the start of this thread. The script is run (somewhat obviously from its name) when you’d normally save a file (i.e., hint reassign Ctrl+s to run this script). If the tab being saved exists as a file in the file system, it’s simply saved as usual. But if the tab has never been saved, a file-save dialog appears and it is based in a fixed directory (as specified in the script by the dir_for_never_saved_files variable).
# -*- coding: utf-8 -*-
#########################################
#
# FileSaveReplacement (FSR)
#
#########################################
# note:
# This script was developed and tested under Python3 64-bit on unicode (non-ANSI) encoded data.
# It may work as-is using Python2 and/or ANSI-encoded data and/or 32-bits, but that would be incidental.
# references:
# https://community.notepad-plus-plus.org/topic/26900/how-to-save-new-documents-always-in-d-data-and-not-in-most-recent-directory
# for newbie info on PythonScripts, see https://community.notepad-plus-plus.org/topic/23039/faq-desk-how-to-install-and-run-a-script-in-pythonscript
# for info on WinDialog (aka Little Dialog Wrapper), see
# https://github.com/Ekopalypse/NppPythonScripts/tree/master/helper/WinDialog
# https://community.notepad-plus-plus.org/topic/24389
#-------------------------------------------------------------------------------
from Npp import *
import os
from WinDialog import * # "Little Dialog Wrapper"
from WinDialog.com_dialogs import FOS
#-------------------------------------------------------------------------------
class FSR(object):
def __init__(self):
# config:
dir_for_never_saved_files = r'd:\data'
ext_for_never_saved_files = '.txt' # make this an empty string if the desire is to have no extension
curr_filename = notepad.getCurrentFilename()
if os.path.isfile(curr_filename) and ('\\' in curr_filename or '/' in curr_filename):
notepad.menuCommand(MENUCOMMAND.FILE_SAVE)
else:
save_dlg = FileSaveDialog()
save_dlg.setFolder(dir_for_never_saved_files)
if '.' not in curr_filename: curr_filename += ext_for_never_saved_files
save_dlg.setFileName(curr_filename)
save_dlg.setFileTypes([['All files', '*.*'], ['Text Files', '*.txt']])
save_options = save_dlg.getOptions()
save_options |= FOS.OVERWRITEPROMPT
save_dlg.setOptions(save_options)
result = save_dlg.show()
if len(result) > 0: notepad.saveAs(result[0])
#-------------------------------------------------------------------------------
if __name__ == '__main__': FSR()