[Dropped] Set file name from first line of document

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
almaf
Posts: 2
Joined: Thu Oct 30, 2025 2:47 pm

[Dropped] Set file name from first line of document

Post by almaf »

The previous thread with this title is locked so cant add reply. Code there didn twork fo rme on Mac so posting here a macro that does work with my setup.

Set LibreOffice (especially Writer) to use the first line of my document as the file name when saving it

Code: Select all

Sub FirstLineFileName_Writer
    On Error goto EH
    oDoc = ThisComponent
    oVC = oDoc.CurrentController.getViewCursor
    Mark = oDoc.Text.CreateTextCursorByRange(oVC) 'mark position of view cursor.
    oTC = oDoc.Text.CreateTextCursor 'created at the beginning of doc.
    While oTC.isEndOfParagraph 'skip empty paragraphs.
        oTC.gotoNextParagraph(false)
    Wend
    oVC.gotoRange(oTC,false) 'a text cursor can't go to the end of a line
    oVC.gotoEndOfLine(true)  'so we have to use the view cursor.
    filename = Trim(oVC.String)

    ' Debug: Print the filename to check for hidden characters
    Print "Filename: '" & filename & "'"

    ' Set the document title to the first line
    oDoc.Title = filename

    ' Open the Save As dialog (filename will be pre-filled)
    oDoc.storeToURL("", Array()) ' This will open the Save As dialog

    oVC.gotoRange(Mark,false) 'return view cursor to original position.
    oDoc.Modified = false 'avoid Save being called if doc closed without further edits.
    End 'end normal execution.
EH: 'error handler.
    MsgBox "You may have illegal file name characters in the first line." & Chr(13) & Chr(13) & filename,,"AN ERROR OCCURRED"
End Sub
Last edited by MrProgrammer on Fri Nov 07, 2025 3:49 pm, edited 1 time in total.
Reason: Dropped: No attachment from almaf; No replies to questions
LibreOffice 25.8. MacOS 14.7.4
JeJe
Volunteer
Posts: 3127
Joined: Wed Mar 09, 2016 2:40 pm

Re: First line of document as file name instead of Untitled 1

Post by JeJe »

Are you posting a solution or still asking help to solve this?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
almaf
Posts: 2
Joined: Thu Oct 30, 2025 2:47 pm

Re: First line of document as file name instead of Untitled 1

Post by almaf »

I thought i was posting a solution but restarted and it doesnt work after all. :(
LibreOffice 25.8. MacOS 14.7.4
JeJe
Volunteer
Posts: 3127
Joined: Wed Mar 09, 2016 2:40 pm

Re: First line of document as file name instead of Untitled 1

Post by JeJe »

Works for me on Windows (can't test on a mac) provided you use the dispatch to call the SaveAs dialog

Code: Select all

dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, Array())
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
MrProgrammer
Moderator
Posts: 5373
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: First line of document as file name instead of Untitled 1

Post by MrProgrammer »

almaf wrote: Thu Oct 30, 2025 9:24 pm I thought i was posting a solution but restarted and it doesnt work after all. :(
You are more likely to get this solved quickly if you attach a document demonstrating the difficulty (remove confidential information then use Post Reply, not Quick Reply, and don't attach a picture instead of the document itself). The problem may be related to the structure of your document, which is why you need to attach it so others can test with it. It doesn't have to be the document you're working with if it is large or contains confidential information, just one which demonstrates the problem you're experiencing. It would help to know exactly why you say: it doesn't work. That phrase doesn't help us understand where the problem might be. Describe all the steps you took to create the problem. We have to assume that you're experiencing a problem with the macro in your first post in this topic, not one you've tried to fix.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.8, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
robleyd
Moderator
Posts: 5465
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: First line of document as file name instead of Untitled 1

Post by robleyd »

What happens if the file already exists? For example, more than one document has the same first line.
Slackware 15 64 bit
Apache OpenOffice 4.1.16
LibreOffice 25.8.3.2; SlackBuild for 25.8.3 by Eric Hameleers
---------------
I hate this damn computer, I wish that I could sell it.
It won't do what I want it to, Only what I tell it.
JeJe
Volunteer
Posts: 3127
Joined: Wed Mar 09, 2016 2:40 pm

Re: First line of document as file name instead of Untitled 1

Post by JeJe »

Are you trying to open the file save as dialog - which is what your comment says, or save the file without it, which is what use of storeToURL suggests? For storeToURL you need a URL not an empty string.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Locked