Default PDF Filename

As a programmer, what would I need to know and look up (and where) in order to add a timestamp (with punctuation deleted or modified, or maybe just the epoch time) to the default filename used for exporting from LibreWriter to PDF? I’d be happy either with being able to change the default appearing in the standard dialog or with being able to create a button that would save directly without a dialog, just using the same directory as the source file (or relative to that directory) and injecting the timecode into the existing source file name when creating the PDF.

What method would be most resilient when doing version updates?

Keep the code below in the LO user directory. To find this location, go to Tools → Options → LibreOffice → Paths and look at AutoCorrect for example. The user directory is kept when updating versions.

For how to run python macros, see Python as a macro language - Apache OpenOffice Wiki. Primarily, this involves putting the code in a new directoryuser/Scripts/python (case must match).

To set up a toolbar button to run the macro, go to Tools → Customize.

import datetime
import uno
from com.sun.star.beans import PropertyValue

def export_pdf_with_timestamp():
    oDoc = XSCRIPTCONTEXT.getDocument()
    sURL = oDoc.getURL()
    ext_len = len(".odt")
    timestamp = "{:%Y-%m-%d_%H-%M-%S}".format(datetime.datetime.now())
    outfilename = "%s_%s.pdf" % (sURL[:-ext_len], timestamp)
    ctx = XSCRIPTCONTEXT.getComponentContext()
    smgr = ctx.ServiceManager
    frame = oDoc.getCurrentController().getFrame()
    dispatcher = smgr.createInstanceWithContext(
        "com.sun.star.frame.DispatchHelper", ctx)
    props = tuple(PropertyValue() for dummy in range(2))
    props[0].Name = "URL"
    props[0].Value = outfilename
    props[1].Name = "FilterName"
    props[1].Value = "writer_pdf_Export"
    dispatcher.executeDispatch(frame, ".uno:ExportToPDF", "", 0, props)

g_exportedScripts = export_pdf_with_timestamp,

Thank you. This looks like it will be straightforward to implement when I get a little break in the coming week. It’s much more thorough help than I expected and the feature will be a big help in my workflow.