Save as from button without dialog

6 views
Skip to first unread message

Jesse Aldridge

unread,
Nov 2, 2008, 8:03:49 PM11/2/08
to leo-editor
Is it possible to do a "save as" with a parameter for the filename
from a script button? I want to do something like:
c.executeMinibufferCommand('save-file-as'), only pass in a second
parameter for the filename instead of having the dialog come up.

Edward K. Ream

unread,
Nov 3, 2008, 7:55:12 AM11/3/08
to leo-e...@googlegroups.com
On Sun, Nov 2, 2008 at 7:03 PM, Jesse Aldridge <JesseA...@gmail.com> wrote:

> Is it possible to do a "save as" with a parameter for the filename
> from a script button?

The answer to all "is it possible" questions is "yes" :-)

There are at least three ways. All the following have been tested.

I'll present my thinking in detail:

1. Cut and paste the relevant code from c.saveAs to your script.

w = g.app.gui.get_focus(c)
c.mFileName = g.ensure_extension(fileName, ".leo")
c.frame.title = c.mFileName
c.frame.setTitle(g.computeWindowTitle(c.mFileName))
c.frame.openDirectory = g.os_path_dirname(c.mFileName) # Bug fix in 4.4b2.
# Calls c.setChanged(False) if no error.
c.fileCommands.saveAs(c.mFileName)
c.updateRecentFiles(c.mFileName)
g.chdir(c.mFileName)
c.redraw()
# c.redraw_after_icons_changed(all=True)
c.widgetWantsFocusNow(w)

2. I dimly recall a discussion about adding arguments to minibuffer
commands. Hmm, now I see a clue. It is c.k.givenArgs in the
saveAsCode.

fileName = ''.join(c.k.givenArgs) or g.app.gui.runSaveFileDialog(
initialfile = c.mFileName,
title="Save As",
filetypes=[("Leo files", "*.leo")],
defaultextension=".leo")

And here is the comment in the leoKeys class:

self.givenArgs = []
# New in Leo 4.4.8: arguments specified after
# the command name in k.simulateCommand.

A little experimentation shows that this works:

fileName = r'c:\prog\test\test2.leo'
c.k.simulateCommand('save-file-as %s' % fileName)

3. Another way would be to temporarily override
g.app.gui.runSaveFileDialog. The safe way to do this is:

fileName = r'c:\prog\test\test3.leo'

def myRun(*args,**keys):
return fileName

old = g.app.gui.runSaveFileDialog
try:
g.app.gui.runSaveFileDialog = myRun
c.saveAs()
finally:
g.app.gui.runSaveFileDialog = old

Edward

Jesse Aldridge

unread,
Nov 3, 2008, 2:08:46 PM11/3/08
to leo-editor
Cool, thanks. That was very informative.

On Nov 3, 6:55 am, "Edward K. Ream" <edream...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages