[Solved] Display Full Path to Document in LibreOffice Window Title

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
LastUnicorn
Posts: 763
Joined: Sat Mar 29, 2008 2:41 am
Location: Scotland

[Solved] Display Full Path to Document in LibreOffice Window Title

Post by LastUnicorn »

I've been struggling with this for over 12 hours and I finally got a macro that works. I got the code for this from Lupp's entry in How to view ‘full path’ of open document?

So working from there the code I ended up with is. (EDIT: Do not use this code it doesn't work well. I have given new code that works below.):

Code: Select all

REM  *****  BASIC  *****
Sub onViewCreated()
showFileLocationInTitleBarSystemStyle(True)
End Sub

Sub onDocumentSavedAs()
showFileLocationInTitleBarSystemStyle(True)
End Sub

Sub showFileLocationInTitleBarSystemStyle()
u = ThisComponent.URL
If u = "" Then Exit Sub REM The document was not yet stored.
ThisComponent.Title = ConvertFromUrl(u)
End Sub 

Sub Main

End Sub
It works after a fashion. However, Lupp notes the following:
 Edit: After some additional considerations, and rgearding specific effects occurring during the debugging of any Basic code, I would dissuade from assigning the macro to Activate document. I assigned it instead to View created and to Document has been saved as as well. So far this works as I like it. 
When I assign the relevant two sub-routines of the macro to the 'events' noted above (in red bold and underlined) they don't work as I think Lupp intended. To get them to work I have to manually (after "saving the document as") do a File > Reload and then the full path to the newly saved document works.

I think what is happening is that when the macro gets to the line u = ThisComponent.URL The value of 'u' is always an empty string (if I can put it that way) and hence the Window Title doesn't end up showing the full path to the file.

Does anyone know how to fix this? I do have a working macro but having to Reload the document after it has been saved is a bit of hassle. I want to avoid having to do a Reload, if that is possible. (I hope I am making my meaning clear.)
Last edited by LastUnicorn on Wed Sep 06, 2023 2:27 pm, edited 2 times in total.
LibreOffice 25.2.3.2 (x64) installed to Windows 11 Pro. 24H2
Apache OpenOffice Portable 4.1.15 [Portable Apps]
For Java I use Adoptium Temurin JRE LTS Releases.
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: Display Full Path to Document in LibreOffice Window Title?

Post by JeJe »

Well you've called your sub "onDocumentSavedAs" so have you assigned it to the saved as event instead of has been saved?

and there is no parameter in your showFileLocationInTitleBarSystemStyle but you've put "(true)" after the calls
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
LastUnicorn
Posts: 763
Joined: Sat Mar 29, 2008 2:41 am
Location: Scotland

Re: Display Full Path to Document in LibreOffice Window Title?

Post by LastUnicorn »

Thank you so very much for the clues JeJe. The code that I had and the Assignments I made were following Lupp's instructions as stated in my original post. It seems that Lupp made a mistake with assigning what Event to what subroutine. In any case after an hour of tinkering with the thing, keeping in mind your comments, I got the following code, which works on my Windows 10 system with LibreOffice and I have also tested it as working in OpenOffice (portable) in the same system.

Code: Select all

REM  *****  BASIC  *****
REM Macro Title: ShowFullPathInWindow (You can give the macro a different name if you so wish.)
Sub onViewCreated REM Assign this sub-routine to 'Event' 'View created'
showFileLocationInTitleBarSystemStyle REM Works with this.
End Sub

Sub onDocumentSavedAs REM Assign this sub-routine to 'Event' 'Document has been saved as'
showFileLocationInTitleBarSystemStyle REM Works with this.
End Sub

Sub showFileLocationInTitleBarSystemStyle
u = ThisComponent.URL
If u = "" Then Exit Sub REM The document was not yet stored.
ThisComponent.Title = ConvertFromUrl(u)
End Sub 

Sub Main

End Sub
Below is a screenshot of the macro Assignments that were made to get this to work. If you want to use this code yourself I'll leave you to work out how to do the assignments — it's pretty straightforward once you work out how.

Macro Events Settings (Annotated).png
Macro Events Settings (Annotated).png (41.64 KiB) Viewed 9232 times
If you haven't worked with macros before the following will give good instruction on how to do what:

To Create/Add a Macro: Getting Started with Macros

For a List of 'Events' That Can be Assigned and how to Assign Them: Document Event-Driven Macros

To get the macro to run at all you might have to specify where on your system documents with macros are stored. To understand that and enable it see this thread: Macro Security "Trusted Sources" clarification?

A Brief Summary of How the Macro Works in Actual Use:
  1. Open LibreOffice so that you are creating a new document — this will automatically have the title of the form Untitled 1
  2. Now that you have the new document loaded do a (menu) File > Save As — and then the window title should automatically show the full path to the document you just saved.
  3. That's it — all done.
Note: All of the above was pretty well new to me and it took me hours to get it to work. I am not a coder of any kind at all but if I can manage it, with some help from this forum, you should be able to do it too. The full path to a document is pretty good information to show in a window title and why LibreOffice/OpenOffice isn't natively coded to do this is beyond me. It has been raised as an enhancement request for LibreOffice in 2014, still not fixed/implemented: Bug 82952 - Add an option to show the full path in the window title. 9 years ago! Ah well, the wheels doth grind exceeding slow.

P.S. I have only been able to test this on Windows 10 and I am now curious if this would work on a Linux system. If anyone has the inclination to give it a try I'd be interested if it worked there too.
LibreOffice 25.2.3.2 (x64) installed to Windows 11 Pro. 24H2
Apache OpenOffice Portable 4.1.15 [Portable Apps]
For Java I use Adoptium Temurin JRE LTS Releases.
Post Reply