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()