import wingapi import os import os.path from wingutils import datatype from guiutils import formbuilder from guiutils import dialogs from guimgr import messages import shutil _AI = wingapi.CArgInfo # ################################################################# # Make a copy of the file in the current editor. Does not # def save_copy_as(target): app = wingapi.gApplication if target: source = app.GetActiveDocument().GetFilename() #Save the source before making a copy? #Uncommewnt next line #app.ExecuteCommand('save') #Abandon if new file that has not been saved. Does not happen if #executing 'save' as above if source.startswith("unknown:"): app.ShowMessageDialog("New File", "New file. Nothing to make a copy of.") return #Unqualified filenames for target will be saved to the scripts directory. #Below makes such names point into the source directory: os.chdir(os.path.split(source)[0]) #Dialog to confirm action #------------------------ def doit(): shutil.copyfile(source, target) if os.path.exists(target): msg = "From %s To: %s\nTarget File exists. Overwrite?" % (source,target) else: msg = "From %s To: %s\nMake the copy?" % (source,target) title = "Save a Copy As" buttons = [ dialogs.CButtonSpec("OK", doit), dialogs.CButtonSpec("Cancel", None), ] dlg = messages.CMessageDialog(wingapi.gApplication.fSingletons, title, msg, (), buttons) dlg.RunAsModal(wingapi.gApplication.fSingletons.fWinMgr.GetActiveWindow()) #Function attributes for save_copy_as. Describes the parameter 'target', #what it is, how to obtain a value for it. save_copy_as.arginfo = {} save_copy_as.arginfo['target'] = _AI("", datatype.CType(''), formbuilder.CFileSelectorGui(), label = ("File Name")) #Where to store the script save_copy_as.contexts = [wingapi.kContextNewMenu("Scripts")] save_copy_as.label = "Save A Copy As ..." #Specifies when script is avaialable def _save_copy_as_available(filename=wingapi.kArgFilename): return filename is not None save_copy_as.available = _save_copy_as_available # ################################################################# # Rename the current file - updating the current project # as needed # def rename_file(target): app = wingapi.gApplication if target: source = app.GetActiveDocument().GetFilename() os.chdir(os.path.split(source)[0]) if os.path.exists(target): app.ShowMessageDialog("File Exists", "Target file %s exists." % target) return app.ExecuteCommand('write-file', filename=target) os.remove(source) project_files = app.GetProject().GetAllFiles() if source in project_files: app.GetProject().AddFiles([target]) app.GetProject().RemoveFiles([source]) rename_file.arginfo = {} rename_file.arginfo['target'] = _AI("", datatype.CType(''), formbuilder.CFileSelectorGui(), label = ("File Name")) rename_file.contexts = [wingapi.kContextNewMenu("Scripts")] rename_file.label = "Rename File ..." def _rename_file_available(filename=wingapi.kArgFilename): return filename is not None rename_file.available = _rename_file_available